Skip to content

Commit edaea3f

Browse files
authored
test(e2e): create fixtures for creating documents and deleting them at the end of a run (#559)
1 parent b2f666a commit edaea3f

File tree

5 files changed

+131
-11
lines changed

5 files changed

+131
-11
lines changed

packages/@repo/e2e/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
"prepare": "pnpm build"
3434
},
3535
"dependencies": {
36+
"@sanity/client": "^6.12.5",
37+
"@sanity/uuid": "^3.0.0",
3638
"dotenv": "^16.5.0"
3739
},
3840
"devDependencies": {

packages/@repo/e2e/src/fixtures.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,35 @@
11
import {test as base} from '@playwright/test'
2+
import {type MultipleMutationResult, SanityClient} from '@sanity/client'
3+
4+
import {getClient} from './helpers/clients'
5+
import {cleanupDocuments, createDocuments, type DocumentStub} from './helpers/documents'
6+
7+
interface SanityFixtures {
8+
createDocuments: (
9+
data: DocumentStub[],
10+
options?: {asDraft?: boolean},
11+
dataset?: string,
12+
) => Promise<MultipleMutationResult>
13+
getClient: (dataset?: string) => SanityClient
14+
}
215

316
/**
417
* @internal
518
* Playwright test configuration for SDK E2E tests
6-
* (If different apps diverge dramatically, we can move this logic)
719
*/
8-
export const test = base.extend({
9-
// We'll add custom fixtures shortly
20+
export const test = base.extend<SanityFixtures>({
21+
// Playwright will error if we don't pass an object to destructure
22+
// eslint-disable-next-line no-empty-pattern
23+
createDocuments: async ({}, use) => {
24+
await use(createDocuments)
25+
26+
// cleanup documents after each test
27+
await cleanupDocuments()
28+
},
29+
// eslint-disable-next-line no-empty-pattern
30+
getClient: async ({}, use) => {
31+
await use(getClient)
32+
},
1033
})
1134

1235
export {expect} from '@playwright/test'
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import {createClient, type SanityClient} from '@sanity/client'
2+
3+
import {getE2EEnv} from './getE2EEnv'
4+
5+
const env = getE2EEnv()
6+
7+
const baseConfig = {
8+
projectId: env.SDK_E2E_PROJECT_ID,
9+
token: env.SDK_E2E_SESSION_TOKEN,
10+
useCdn: false,
11+
apiVersion: '2021-08-31',
12+
apiHost: 'https://api.sanity.work',
13+
}
14+
15+
const clients: Record<string, SanityClient> = {}
16+
17+
export function getClient(dataset: string = env.SDK_E2E_DATASET_0): SanityClient {
18+
if (!clients[dataset]) {
19+
clients[dataset] = createClient({
20+
...baseConfig,
21+
dataset,
22+
})
23+
}
24+
25+
return clients[dataset]
26+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import {type MultipleMutationResult, type SanityDocumentStub} from '@sanity/client'
2+
import {uuid} from '@sanity/uuid'
3+
4+
import {getClient} from './clients'
5+
import {getE2EEnv} from './getE2EEnv'
6+
7+
const env = getE2EEnv()
8+
9+
export type DocumentStub = Omit<SanityDocumentStub, '_id'>
10+
11+
// Keep track of document IDs created during tests
12+
const documentIds = new Set<string>()
13+
14+
function getUniqueDocumentId(): string {
15+
const documentId = uuid()
16+
documentIds.add(documentId)
17+
return documentId
18+
}
19+
20+
export async function createDocuments<T extends DocumentStub>(
21+
data: T[],
22+
options?: {asDraft?: boolean},
23+
dataset?: string,
24+
): Promise<MultipleMutationResult> {
25+
const client = getClient(dataset)
26+
const asDraft = options?.asDraft ?? true // Default to draft if not specified
27+
28+
const docs = data.map((doc) => {
29+
const documentId = getUniqueDocumentId()
30+
return {
31+
...doc,
32+
_type: doc['_type'],
33+
_id: asDraft ? `drafts.${documentId}` : documentId,
34+
}
35+
})
36+
37+
const transaction = client.transaction()
38+
docs.forEach((doc) => transaction.create(doc))
39+
return transaction.commit()
40+
}
41+
42+
export async function cleanupDocuments(): Promise<void> {
43+
if (documentIds.size === 0) return
44+
45+
// Clean up documents in both datasets
46+
await Promise.all(
47+
[env.SDK_E2E_DATASET_0, env.SDK_E2E_DATASET_1].map(async (dataset) => {
48+
try {
49+
const client = getClient(dataset)
50+
await client.delete({
51+
query: '*[_id in $ids]',
52+
params: {ids: [...[...documentIds].map((id) => `drafts.${id}`), ...documentIds]},
53+
})
54+
} catch (error) {
55+
// Log error but don't fail teardown
56+
// eslint-disable-next-line no-console
57+
console.error(`Failed to clean up documents in ${dataset}:`, error)
58+
}
59+
}),
60+
)
61+
62+
documentIds.clear()
63+
}

pnpm-lock.yaml

Lines changed: 14 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)