Visitar URL original
chore: dedupe and clean up tests · nuxt-modules/shopify@6ac14a6 · GitHub
Skip to content

Commit 6ac14a6

Browse files
committed
chore: dedupe and clean up tests
Signed-off-by: Frederik Bußmann <frederik@bussmann.io>
1 parent cfc502a commit 6ac14a6

49 files changed

Lines changed: 651 additions & 1986 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎test/e2e/v4-mock.test.ts‎

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { describe, expect, it } from 'vitest'
2-
import { setup, $fetch, fetch, useTestContext } from '@nuxt/test-utils'
3-
import { existsSync } from 'node:fs'
4-
import { fileURLToPath, pathToFileURL } from 'node:url'
5-
import { join } from 'node:path'
2+
import { setup, $fetch, fetch } from '@nuxt/test-utils'
3+
import { fileURLToPath } from 'node:url'
64

75
import { DEFAULT_API_VERSION } from '#src/runtime/utils/clients/defaults'
86

@@ -42,28 +40,9 @@ describe('test mock.shop integration with nuxt 4', async () => {
4240
expect(response.headers.get('location')).toBe('/product/shirt?ref=email')
4341
})
4442

45-
it('should redirect /admin to the store\'s admin', async () => {
46-
const response = await fetch('/admin', { redirect: 'manual' })
47-
48-
expect(response.status).toBe(301)
49-
expect(response.headers.get('location')).toBe('https://mock.shop/admin')
50-
})
51-
5243
it('should keep the not found page when Shopify knows no redirect', async () => {
5344
const response = await fetch('/no-such-page', { redirect: 'manual', headers: { accept: 'text/html' } })
5445

5546
expect(response.status).toBe(404)
5647
})
57-
58-
it('should point the graphql config at the schema the build introspected', async () => {
59-
const { rootDir, buildDir } = useTestContext().nuxt!.options
60-
61-
const { default: graphqlConfig } = await import(/* @vite-ignore */ pathToFileURL(join(buildDir, 'graphql.config.mjs')).href)
62-
63-
const projects = Object.entries<{ schema: string }>(graphqlConfig.projects)
64-
65-
expect(Object.fromEntries(projects.map(([name, { schema }]) => [name, existsSync(join(rootDir, schema))]))).toEqual({
66-
default: true,
67-
})
68-
})
6948
})

‎test/e2e/v4.test.ts‎

Lines changed: 25 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -164,52 +164,33 @@ describe('test module with nuxt 4', async () => {
164164
expect(response.status).toBe(401)
165165
})
166166

167-
it('should generate storefront api operations', async () => {
168-
const operations = await readBuildFile('shopify/storefront/storefront.operations.d.ts')
169-
170-
expect(operations).toContain('export type FetchAsyncProductsQuery =')
171-
expect(operations).toContain('export type FetchProductsQuery =')
172-
expect(operations).toContain('export type FetchFirstThreeProductsQuery =')
173-
expect(operations).toContain('export type FetchProductsWithErrorQuery =')
174-
expect(operations).toContain('export type ProductFieldsFragment =')
175-
176-
expect(operations).toContain([
177-
`declare module '@nuxtjs/shopify/storefront' {`,
178-
' type InputMaybe<T> = StorefrontTypes.InputMaybe<T>',
179-
' interface StorefrontQueries extends GeneratedQueryTypes {}',
180-
' interface StorefrontMutations extends GeneratedMutationTypes {}',
181-
'}',
182-
].join('\n'))
183-
184-
expect(operations).toContain('declare global {')
185-
})
186-
187-
it('should generate admin api operations', async () => {
188-
const operations = await readBuildFile('shopify/admin/admin.operations.d.ts')
189-
190-
expect(operations).toContain('export type FetchMarketsQuery =')
191-
expect(operations).toContain('export type MarketFieldsFragment =')
192-
193-
expect(operations).toContain([
194-
`declare module '@nuxtjs/shopify/admin' {`,
195-
' type InputMaybe<T> = AdminTypes.InputMaybe<T>',
196-
' interface AdminQueries extends GeneratedQueryTypes {}',
197-
' interface AdminMutations extends GeneratedMutationTypes {}',
198-
'}',
199-
].join('\n'))
200-
})
201-
202-
it('should generate customer account api operations', async () => {
203-
const operations = await readBuildFile('shopify/customer-account/customer-account.operations.d.ts')
204-
205-
expect(operations).toContain('export type CustomerDetailsQuery =')
206-
expect(operations).toContain('export type CustomerFieldsFragment =')
167+
it.each([
168+
['storefront', 'storefront', 'Storefront', [
169+
'export type FetchAsyncProductsQuery =',
170+
'export type FetchProductsQuery =',
171+
'export type FetchFirstThreeProductsQuery =',
172+
'export type FetchProductsWithErrorQuery =',
173+
'export type ProductFieldsFragment =',
174+
'declare global {',
175+
]],
176+
['admin', 'admin', 'Admin', [
177+
'export type FetchMarketsQuery =',
178+
'export type MarketFieldsFragment =',
179+
]],
180+
['customer account', 'customer-account', 'CustomerAccount', [
181+
'export type CustomerDetailsQuery =',
182+
'export type CustomerFieldsFragment =',
183+
]],
184+
])('should generate %s api operations', async (_, client, namespace, declarations) => {
185+
const operations = await readBuildFile(`shopify/${client}/${client}.operations.d.ts`)
186+
187+
for (const declaration of declarations) expect(operations).toContain(declaration)
207188

208189
expect(operations).toContain([
209-
`declare module '@nuxtjs/shopify/customer-account' {`,
210-
' type InputMaybe<T> = CustomerAccountTypes.InputMaybe<T>',
211-
' interface CustomerAccountQueries extends GeneratedQueryTypes {}',
212-
' interface CustomerAccountMutations extends GeneratedMutationTypes {}',
190+
`declare module '@nuxtjs/shopify/${client}' {`,
191+
` type InputMaybe<T> = ${namespace}Types.InputMaybe<T>`,
192+
` interface ${namespace}Queries extends GeneratedQueryTypes {}`,
193+
` interface ${namespace}Mutations extends GeneratedMutationTypes {}`,
213194
'}',
214195
].join('\n'))
215196
})

‎test/helpers/customer-account.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ export function createHydrogenSession(tokens: HydrogenTokens = {}) {
3232
}
3333
}
3434

35+
export const EXPIRED: HydrogenTokens = { expiresAt: Date.now() - 1000 }
36+
3537
function base64Url(value: string): string {
3638
return Buffer.from(value).toString('base64url')
3739
}
@@ -68,7 +70,7 @@ export function createShopifyConfig(customerAccount: Record<string, unknown> = {
6870

6971
export const UNCONFIGURED_SHOPIFY_CONFIG = { name: 'test-shop', clients: {} }
7072

71-
export function createMemoryStorage() {
73+
function createMemoryStorage() {
7274
const items = new Map<string, unknown>()
7375

7476
return {

‎test/helpers/fetch.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { vi } from 'vitest'
2+
3+
export function stubHangingFetch() {
4+
const fetchMock = vi.fn((_url: string, init?: RequestInit) => new Promise<Response>((_resolve, reject) => {
5+
if (init?.signal?.aborted) reject(init.signal.reason)
6+
7+
init?.signal?.addEventListener('abort', () => reject(init.signal!.reason))
8+
}))
9+
10+
vi.stubGlobal('fetch', fetchMock)
11+
12+
return fetchMock
13+
}

‎test/helpers/logger.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,9 @@ type LoggerMethod = 'start' | 'success' | 'debug' | 'info' | 'warn' | 'error'
55
export function createLoggerStub(overrides: Partial<Record<LoggerMethod, (...args: never[]) => unknown>> = {}) {
66
return { start: vi.fn(), success: vi.fn(), debug: vi.fn(), info: vi.fn(), warn: vi.fn(), error: vi.fn(), ...overrides }
77
}
8+
9+
export function createLogReporter() {
10+
const log = vi.fn()
11+
12+
return { log, logger: { reporters: [{ log }] } }
13+
}

‎test/helpers/stubs.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
11
export { createError } from 'h3'
2-
3-
export const useHead = () => {}

‎test/unit/commands/webhooks.test.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,13 @@ describe('getShopifyConfig', () => {
2525
it('loads the app like a build, so an invalid webhooks config fails the command instead of being skipped', async () => {
2626
await expect(getShopifyConfig()).resolves.toEqual({ name: 'shop' })
2727

28-
expect(loadNuxt).toHaveBeenCalledExactlyOnceWith({ dotenv: true })
28+
expect(loadNuxt).toHaveBeenCalledOnce()
29+
30+
const [options] = loadNuxt.mock.calls[0]!
31+
32+
expect(options).not.toHaveProperty('dev', true)
33+
expect(options).not.toHaveProperty('overrides._prepare', true)
34+
expect(options).not.toHaveProperty('dotenv', false)
2935
})
3036

3137
it('closes the app it loaded, also when the module is not configured', async () => {

‎test/unit/runtime/composables/async.test.ts‎

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -81,18 +81,6 @@ describe('async data variables', () => {
8181

8282
expect(requests.map(request => request.variables)).toStrictEqual([{ handle: 'first' }, { handle: 'second' }])
8383
})
84-
85-
it.each(clients)('leaves the caller\'s $name variables object untouched', async ({ useData }) => {
86-
const handle = ref('first')
87-
const variables = { handle }
88-
89-
const { refresh } = useData('key', 'query', { variables })
90-
91-
await refresh()
92-
93-
expect(variables.handle).toBe(handle)
94-
expect(handle.value).toBe('first')
95-
})
9684
})
9785

9886
describe('async data requests', () => {

‎test/unit/runtime/composables/customer-account/client.test.ts‎

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -68,32 +68,15 @@ describe('useCustomerAccount', () => {
6868
expect(getAccessToken).toHaveBeenCalledExactlyOnceWith(event)
6969
})
7070

71-
it('goes through the proxy in the browser, without a token of its own', () => {
72-
vi.stubGlobal('__NUXT_CLIENT__', true)
73-
74-
useCustomerAccount()
75-
76-
expect(lastOptions().origin).toBe('https://shop.test')
77-
expect(lastOptions().auth).toBeUndefined()
78-
})
79-
80-
it('keeps going through the proxy after a prerendered landing page', () => {
81-
vi.stubGlobal('__NUXT_CLIENT__', true)
82-
83-
payload.prerenderedAt = Date.now()
84-
85-
useCustomerAccount()
86-
87-
expect(lastOptions().origin).toBe('https://shop.test')
88-
})
89-
90-
it('reaches the proxy at the domain root, next to the sign-in routes, even under an app base URL', () => {
71+
it('goes through the proxy at the domain root in the browser, without a token of its own, even under an app base URL or after a prerendered landing page', () => {
9172
vi.stubGlobal('__NUXT_CLIENT__', true)
9273

9374
baseURL = '/shop/'
75+
payload.prerenderedAt = Date.now()
9476

9577
useCustomerAccount()
9678

9779
expect(lastOptions().origin).toBe('https://shop.test')
80+
expect(lastOptions().auth).toBeUndefined()
9881
})
9982
})

‎test/unit/runtime/composables/storefront/client.test.ts‎

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -63,31 +63,14 @@ describe('useStorefront', () => {
6363
expect(createStorefrontApiClient.mock.calls.at(-1)?.[0]).toBe(publicConfig)
6464
})
6565

66-
it('reaches Shopify through its own origin in the browser, without a server cache', () => {
67-
vi.stubGlobal('__NUXT_CLIENT__', true)
68-
69-
useStorefront()
70-
71-
expect(lastOptions()).toMatchObject({ origin: 'https://app.example.workers.dev', serverCache: undefined })
72-
})
73-
74-
it('reaches Hydrogen\'s proxy at the domain root, where its routes are served, even under an app base URL', () => {
66+
it('reaches Hydrogen\'s proxy at the domain root in the browser, without a server cache, even under an app base URL or after a prerendered landing page', () => {
7567
vi.stubGlobal('__NUXT_CLIENT__', true)
7668

7769
baseURL = '/shop/'
78-
79-
useStorefront()
80-
81-
expect(lastOptions()).toMatchObject({ origin: 'https://app.example.workers.dev' })
82-
})
83-
84-
it('keeps going through the proxy after a prerendered landing page', () => {
85-
vi.stubGlobal('__NUXT_CLIENT__', true)
86-
8770
payload.prerenderedAt = Date.now()
8871

8972
useStorefront()
9073

91-
expect(lastOptions()).toMatchObject({ origin: 'https://app.example.workers.dev' })
74+
expect(lastOptions()).toMatchObject({ origin: 'https://app.example.workers.dev', serverCache: undefined })
9275
})
9376
})

0 commit comments

Comments
 (0)