This repository was archived by the owner on Apr 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 389
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1047 from Shopify/ml-sfapi-client-segment-tests
[Storefront API Client] Reorganize tests for server and browser testing
- @shopify/storefront-api-client@0.3.3
- @shopify/storefront-api-client@0.3.2
- @shopify/storefront-api-client@0.3.1
- @shopify/storefront-api-client@0.3.0
- @shopify/storefront-api-client@0.2.4
- @shopify/storefront-api-client@0.2.3
- @shopify/storefront-api-client@0.2.2
- @shopify/storefront-api-client@0.2.1
- @shopify/storefront-api-client@0.2.0
- @shopify/storefront-api-client@0.1.1
- @shopify/shopify-api@9.7.1
- @shopify/shopify-api@9.7.0
- @shopify/shopify-api@9.6.2
- @shopify/shopify-api@9.6.1
- @shopify/shopify-api@9.6.0
- @shopify/shopify-api@9.5.1
- @shopify/shopify-api@9.5.0
- @shopify/shopify-api@9.4.1
- @shopify/shopify-api@9.4.0
- @shopify/shopify-api@9.3.2
- @shopify/shopify-api@9.3.1
- @shopify/shopify-api@9.3.0
- @shopify/shopify-api@9.2.0
- @shopify/shopify-api@9.1.0
- @shopify/shopify-api@9.0.2
- @shopify/shopify-api@9.0.1
- @shopify/shopify-api@9.0.0
- @shopify/shopify-api@8.1.1
- @shopify/shopify-api@8.1.0
- @shopify/graphql-client@0.10.3
- @shopify/graphql-client@0.10.2
- @shopify/graphql-client@0.10.1
- @shopify/graphql-client@0.10.0
- @shopify/graphql-client@0.9.4
- @shopify/graphql-client@0.9.3
- @shopify/graphql-client@0.9.2
- @shopify/graphql-client@0.9.1
- @shopify/graphql-client@0.9.0
- @shopify/graphql-client@0.8.0
- @shopify/api-codegen-preset@0.0.7
- @shopify/api-codegen-preset@0.0.6
- @shopify/api-codegen-preset@0.0.5
- @shopify/api-codegen-preset@0.0.4
- @shopify/api-codegen-preset@0.0.3
- @shopify/api-codegen-preset@0.0.2
- @shopify/api-codegen-preset@0.0.1
- @shopify/admin-api-client@0.2.8
- @shopify/admin-api-client@0.2.7
- @shopify/admin-api-client@0.2.6
- @shopify/admin-api-client@0.2.5
- @shopify/admin-api-client@0.2.4
- @shopify/admin-api-client@0.2.3
- @shopify/admin-api-client@0.2.2
- @shopify/admin-api-client@0.2.1
- @shopify/admin-api-client@0.2.0
- @shopify/admin-api-client@0.1.0
Showing
11 changed files
with
212 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
--- | ||
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
packages/storefront-api-client/src/tests/storefront-api-client/fixtures.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { GraphQLClient } from "@shopify/graphql-client"; | ||
|
||
export const mockApiVersions = [ | ||
"2023-01", | ||
"2023-04", | ||
"2023-07", | ||
"2023-10", | ||
"2024-01", | ||
"unstable", | ||
]; | ||
|
||
export const domain = "test-store.myshopify.io"; | ||
export const config = { | ||
storeDomain: `https://${domain}`, | ||
apiVersion: "2023-10", | ||
publicAccessToken: "public-token", | ||
}; | ||
|
||
export const mockApiUrl = `${config.storeDomain}/api/2023-10/graphql.json`; | ||
|
||
export const graphqlClientMock: GraphQLClient = { | ||
config: { | ||
url: mockApiUrl, | ||
headers: {}, | ||
retries: 0, | ||
}, | ||
fetch: jest.fn(), | ||
request: jest.fn(), | ||
}; |
44 changes: 44 additions & 0 deletions
44
...orefront-api-client/src/tests/storefront-api-client/storefront-api-client.browser.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { createGraphQLClient } from "@shopify/graphql-client"; | ||
|
||
import { createStorefrontApiClient } from "../../storefront-api-client"; | ||
|
||
import { mockApiVersions, graphqlClientMock, config } from "./fixtures"; | ||
|
||
jest.mock("@shopify/graphql-client", () => { | ||
return { | ||
...jest.requireActual("@shopify/graphql-client"), | ||
createGraphQLClient: jest.fn(), | ||
getCurrentSupportedAPIVersions: () => mockApiVersions, | ||
}; | ||
}); | ||
|
||
describe("Storefront API Client: Browser", () => { | ||
describe("createStorefrontApiClient()", () => { | ||
beforeEach(() => { | ||
(createGraphQLClient as jest.Mock).mockReturnValue(graphqlClientMock); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
describe("client initialization", () => { | ||
describe("validations", () => { | ||
it("throws an error when a private access token is provided in a browser environment", () => { | ||
expect(() => | ||
createStorefrontApiClient({ | ||
...config, | ||
publicAccessToken: undefined as any, | ||
privateAccessToken: "private-access-token", | ||
}) | ||
).toThrow( | ||
new Error( | ||
"Storefront API Client: private access tokens and headers should only be used in a server-to-server implementation. Use the public API access token in nonserver environments." | ||
) | ||
); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
74 changes: 74 additions & 0 deletions
74
...torefront-api-client/src/tests/storefront-api-client/storefront-api-client.server.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { createGraphQLClient } from "@shopify/graphql-client"; | ||
|
||
import { createStorefrontApiClient } from "../../storefront-api-client"; | ||
import { PRIVATE_ACCESS_TOKEN_HEADER } from "../../constants"; | ||
|
||
import { mockApiVersions, graphqlClientMock, config } from "./fixtures"; | ||
|
||
jest.mock("@shopify/graphql-client", () => { | ||
return { | ||
...jest.requireActual("@shopify/graphql-client"), | ||
createGraphQLClient: jest.fn(), | ||
getCurrentSupportedAPIVersions: () => mockApiVersions, | ||
}; | ||
}); | ||
|
||
describe("Storefront API Client: Server", () => { | ||
describe("createStorefrontApiClient()", () => { | ||
beforeEach(() => { | ||
(createGraphQLClient as jest.Mock).mockReturnValue(graphqlClientMock); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
describe("client initialization", () => { | ||
describe("validations", () => { | ||
it("throws an error when both public and private access tokens are provided in a server environment", () => { | ||
expect(() => | ||
createStorefrontApiClient({ | ||
...config, | ||
privateAccessToken: "private-token", | ||
} as any) | ||
).toThrow( | ||
new Error( | ||
`Storefront API Client: only provide either a public or private access token` | ||
) | ||
); | ||
}); | ||
}); | ||
}); | ||
|
||
describe("client config", () => { | ||
it("returns a config object that includes the provided private access token and not a public access token when in a server environment", () => { | ||
const privateAccessToken = "private-token"; | ||
|
||
const client = createStorefrontApiClient({ | ||
...config, | ||
publicAccessToken: undefined, | ||
privateAccessToken, | ||
}); | ||
expect(client.config.privateAccessToken).toBe(privateAccessToken); | ||
expect(client.config.publicAccessToken).toBeUndefined(); | ||
}); | ||
|
||
describe("config headers", () => { | ||
it("returns a header object that includes the private headers when a private access token is provided", () => { | ||
const privateAccessToken = "private-token"; | ||
|
||
const client = createStorefrontApiClient({ | ||
...config, | ||
publicAccessToken: undefined, | ||
privateAccessToken, | ||
}); | ||
|
||
expect(client.config.headers[PRIVATE_ACCESS_TOKEN_HEADER]).toEqual( | ||
privateAccessToken | ||
); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 0 additions & 43 deletions
43
packages/storefront-api-client/src/tests/validations.test.ts
This file was deleted.
Oops, something went wrong.
13 changes: 13 additions & 0 deletions
13
packages/storefront-api-client/src/tests/validations/validations.browser.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { validatePrivateAccessTokenUsage } from "../../validations"; | ||
|
||
describe("validatePrivateAccessTokenUsage(): Browser", () => { | ||
it("throws an error when a private token is provided within a browser environment (window is defined)", () => { | ||
const privateAccessToken = "private-token"; | ||
|
||
expect(() => validatePrivateAccessTokenUsage(privateAccessToken)).toThrow( | ||
new Error( | ||
"Storefront API Client: private access tokens and headers should only be used in a server-to-server implementation. Use the public API access token in nonserver environments." | ||
) | ||
); | ||
}); | ||
}); |
11 changes: 11 additions & 0 deletions
11
packages/storefront-api-client/src/tests/validations/validations.server.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { validatePrivateAccessTokenUsage } from "../../validations"; | ||
|
||
describe("validatePrivateAccessTokenUsage(): Server", () => { | ||
it("does not throw an error when only the private token is provided when within a server environment", () => { | ||
const privateAccessToken = "private-token"; | ||
|
||
expect(() => | ||
validatePrivateAccessTokenUsage(privateAccessToken) | ||
).not.toThrow(); | ||
}); | ||
}); |
16 changes: 16 additions & 0 deletions
16
packages/storefront-api-client/src/tests/validations/validations.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { validateRequiredAccessTokens } from "../../validations"; | ||
|
||
describe("validateRequiredAccessToken()", () => { | ||
it("throws an error when both public and private tokens are undefined", () => { | ||
const publicAccessToken = undefined; | ||
const privateAccessToken = undefined; | ||
|
||
expect(() => | ||
validateRequiredAccessTokens(publicAccessToken, privateAccessToken) | ||
).toThrow( | ||
new Error( | ||
"Storefront API Client: a public or private access token must be provided" | ||
) | ||
); | ||
}); | ||
}); |