-
Notifications
You must be signed in to change notification settings - Fork 435
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: prep work for making e2e tests more isolated (#4961)
- Loading branch information
Showing
5 changed files
with
182 additions
and
170 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
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 |
---|---|---|
@@ -1,18 +1,16 @@ | ||
import {SanityDocument, SanityDocumentStub} from '@sanity/client' | ||
import {SanityClient, SanityDocument, SanityDocumentStub} from '@sanity/client' | ||
import {uuid} from '@sanity/uuid' | ||
import {testSanityClient} from './sanityClient' | ||
|
||
export async function createUniqueDocument({ | ||
_type, | ||
_id, | ||
...restProps | ||
}: SanityDocumentStub): Promise<Partial<SanityDocument>> { | ||
export async function createUniqueDocument( | ||
client: SanityClient, | ||
{_type, _id, ...restProps}: SanityDocumentStub, | ||
): Promise<Partial<SanityDocument>> { | ||
const doc = { | ||
_type, | ||
_id: _id || uuid(), | ||
...restProps, | ||
} | ||
|
||
await testSanityClient.create(doc, {visibility: 'async'}) | ||
await client.create(doc, {visibility: 'async'}) | ||
return doc | ||
} |
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 |
---|---|---|
@@ -1,22 +1,44 @@ | ||
import {createClient} from '@sanity/client' | ||
import {SanityClient} from 'sanity' | ||
import {SANITY_E2E_SESSION_TOKEN} from '../env' | ||
import {STALE_TEST_THRESHOLD_MS, STUDIO_DATASET_NAME, STUDIO_PROJECT_ID} from './constants' | ||
import {STUDIO_DATASET_NAME, STUDIO_PROJECT_ID} from './constants' | ||
import {uuid} from '@sanity/uuid' | ||
|
||
export const testSanityClient = createClient({ | ||
export class TestContext { | ||
client: SanityClient | ||
|
||
constructor(client: SanityClient) { | ||
this.client = client | ||
} | ||
|
||
documentIds = new Set<string>() | ||
|
||
getUniqueDocumentId() { | ||
const documentId = uuid() | ||
this.documentIds.add(documentId) | ||
return documentId | ||
} | ||
|
||
teardown() { | ||
this.client.delete({ | ||
query: '*[_id in $ids]', | ||
params: {ids: [...this.documentIds].map((id) => `drafts.${id}`)}, | ||
}) | ||
} | ||
} | ||
|
||
const testSanityClient = createClient({ | ||
projectId: STUDIO_PROJECT_ID, | ||
dataset: STUDIO_DATASET_NAME, | ||
token: SANITY_E2E_SESSION_TOKEN, | ||
useCdn: false, | ||
apiVersion: '2021-08-31', | ||
}) | ||
|
||
export function deleteDocumentsForRun( | ||
typeName: string, | ||
runId: string, | ||
): {query: string; params: Record<string, unknown>} { | ||
const threshold = new Date(Date.now() - STALE_TEST_THRESHOLD_MS).toISOString() | ||
return { | ||
query: `*[_type == $typeName && (runId == $runId || _createdAt < "${threshold}")]`, | ||
params: {typeName, runId}, | ||
} | ||
/* eslint-disable callback-return*/ | ||
export function withDefaultClient(callback: (context: TestContext) => void): void { | ||
const context = new TestContext(testSanityClient) | ||
callback(context) | ||
|
||
context.teardown() | ||
} |
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 |
---|---|---|
@@ -1,85 +1,90 @@ | ||
/* eslint-disable max-nested-callbacks */ | ||
import {test, expect} from '@playwright/test' | ||
import {createUniqueDocument, testSanityClient} from '../../helpers' | ||
|
||
test.describe.skip('sanity/desk: document inspectors', () => { | ||
test('open and close custom inspector', async ({page}) => { | ||
await page.goto('/test/content/input-debug;inspectorsTest;inspectors-test') | ||
|
||
// Click to open inspector | ||
await page | ||
.locator('[data-ui="StatusButton"][aria-label="Custom inspector"]') | ||
.click({timeout: 0}) | ||
|
||
// Expect button to be selected and inspector to be visible | ||
await expect( | ||
page.locator('[data-ui="StatusButton"][aria-label="Custom inspector"][data-selected]'), | ||
).toBeVisible() | ||
await expect(page.locator('aside[data-ui="DocumentInspectorPanel"]')).toBeVisible() | ||
await expect(page.locator('aside[data-ui="DocumentInspectorPanel"] h1')).toContainText( | ||
'Custom inspector', | ||
) | ||
|
||
// Click to close inspector | ||
await page.locator('button[aria-label="Close custom inspector"]').click() | ||
|
||
expect( | ||
await page | ||
.locator('[data-ui="StatusButton"][aria-label="Custom inspector"]') | ||
.evaluate((el) => el.getAttribute('data-selected')), | ||
).toBe(null) | ||
}) | ||
import {createUniqueDocument, withDefaultClient} from '../../helpers' | ||
|
||
test('open "Validation" inspector', async ({page}) => { | ||
// create published document | ||
const uniqueDoc = await createUniqueDocument({_type: 'validationTest'}) | ||
const id = uniqueDoc._id! | ||
withDefaultClient((context) => { | ||
test.describe.skip('sanity/desk: document inspectors', () => { | ||
test('open and close custom inspector', async ({page}) => { | ||
await page.goto('/test/content/input-debug;inspectorsTest;inspectors-test') | ||
|
||
// create draft document | ||
await createUniqueDocument({ | ||
_type: 'inspectorsTest', | ||
_id: `drafts.${id}`, | ||
name: 'Edited by e2e test runner', | ||
// Click to open inspector | ||
await page | ||
.locator('[data-ui="StatusButton"][aria-label="Custom inspector"]') | ||
.click({timeout: 0}) | ||
|
||
// Expect button to be selected and inspector to be visible | ||
await expect( | ||
page.locator('[data-ui="StatusButton"][aria-label="Custom inspector"][data-selected]'), | ||
).toBeVisible() | ||
await expect(page.locator('aside[data-ui="DocumentInspectorPanel"]')).toBeVisible() | ||
await expect(page.locator('aside[data-ui="DocumentInspectorPanel"] h1')).toContainText( | ||
'Custom inspector', | ||
) | ||
|
||
// Click to close inspector | ||
await page.locator('button[aria-label="Close custom inspector"]').click() | ||
|
||
expect( | ||
await page | ||
.locator('[data-ui="StatusButton"][aria-label="Custom inspector"]') | ||
.evaluate((el) => el.getAttribute('data-selected')), | ||
).toBe(null) | ||
}) | ||
|
||
await page.goto(`/test/content/input-debug;validationTest;${id}`) | ||
|
||
// Click to open inspector | ||
await page.locator('[data-ui="StatusButton"][aria-label="Validation"]').click({timeout: 0}) | ||
|
||
// Expect button to be selected and inspector to be visible | ||
await expect( | ||
page.locator('[data-ui="StatusButton"][aria-label="Validation"][data-selected]'), | ||
).toBeVisible() | ||
await expect(page.locator('aside[data-ui="DocumentInspectorPanel"]')).toBeVisible() | ||
await expect(page.locator('aside[data-ui="DocumentInspectorPanel"] h1')).toContainText( | ||
'Validation', | ||
) | ||
}) | ||
|
||
test('open "Review changes" inspector', async ({page}) => { | ||
// create published document | ||
const uniqueDoc = await createUniqueDocument({_type: 'inspectorsTest'}) | ||
const id = uniqueDoc._id! | ||
|
||
// create draft document | ||
await createUniqueDocument({ | ||
_type: 'inspectorsTest', | ||
_id: `drafts.${id}`, | ||
name: 'Edited by e2e test runner', | ||
test('open "Validation" inspector', async ({page}) => { | ||
// create published document | ||
const uniqueDoc = await createUniqueDocument(context.client, {_type: 'validationTest'}) | ||
const id = uniqueDoc._id! | ||
|
||
// create draft document | ||
await createUniqueDocument(context.client, { | ||
_type: 'inspectorsTest', | ||
_id: `drafts.${id}`, | ||
name: 'Edited by e2e test runner', | ||
}) | ||
|
||
await page.goto(`/test/content/input-debug;validationTest;${id}`) | ||
|
||
// Click to open inspector | ||
await page.locator('[data-ui="StatusButton"][aria-label="Validation"]').click({timeout: 0}) | ||
|
||
// Expect button to be selected and inspector to be visible | ||
await expect( | ||
page.locator('[data-ui="StatusButton"][aria-label="Validation"][data-selected]'), | ||
).toBeVisible() | ||
await expect(page.locator('aside[data-ui="DocumentInspectorPanel"]')).toBeVisible() | ||
await expect(page.locator('aside[data-ui="DocumentInspectorPanel"] h1')).toContainText( | ||
'Validation', | ||
) | ||
}) | ||
|
||
await page.goto(`/test/content/input-debug;inspectorsTest;${id}`) | ||
|
||
// Click to open inspector | ||
await page.locator('[data-testid="review-changes-button"]').click() | ||
|
||
// Expect button to be selected and inspector to be visible | ||
await expect(page.locator('[data-testid="review-changes-button"][data-selected]')).toBeVisible() | ||
await expect(page.locator('aside[data-ui="DocumentInspectorPanel"]')).toBeVisible() | ||
await expect(page.locator('aside[data-ui="DocumentInspectorPanel"] h1')).toContainText( | ||
'Review changes', | ||
) | ||
|
||
await testSanityClient.delete(id) | ||
test('open "Review changes" inspector', async ({page}) => { | ||
// create published document | ||
const uniqueDoc = await createUniqueDocument(context.client, {_type: 'inspectorsTest'}) | ||
const id = uniqueDoc._id! | ||
|
||
// create draft document | ||
await createUniqueDocument(context.client, { | ||
_type: 'inspectorsTest', | ||
_id: `drafts.${id}`, | ||
name: 'Edited by e2e test runner', | ||
}) | ||
|
||
await page.goto(`/test/content/input-debug;inspectorsTest;${id}`) | ||
|
||
// Click to open inspector | ||
await page.locator('[data-testid="review-changes-button"]').click() | ||
|
||
// Expect button to be selected and inspector to be visible | ||
await expect( | ||
page.locator('[data-testid="review-changes-button"][data-selected]'), | ||
).toBeVisible() | ||
await expect(page.locator('aside[data-ui="DocumentInspectorPanel"]')).toBeVisible() | ||
await expect(page.locator('aside[data-ui="DocumentInspectorPanel"] h1')).toContainText( | ||
'Review changes', | ||
) | ||
|
||
await context.client.delete(id) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.
241402a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
performance-studio – ./
performance-studio-git-next.sanity.build
performance-studio.sanity.build
241402a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
test-studio – ./
test-studio-git-next.sanity.build
test-studio.sanity.build