-
Notifications
You must be signed in to change notification settings - Fork 448
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(desk-tool): port document actions tests to playwright
- Loading branch information
Showing
14 changed files
with
197 additions
and
10 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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export default { | ||
name: 'documentActionsCI', | ||
title: 'Document Actions CI', | ||
type: 'document', | ||
fields: [ | ||
{ | ||
name: 'title', | ||
title: 'Title', | ||
type: 'string', | ||
}, | ||
], | ||
} |
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
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
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
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export const DEFAULT_DATASET_NAME = 'test' | ||
export const STUDIO_SPACE_NAME = 'test' | ||
export const STUDIO_PROJECT_ID = 'ppsg7ml5' | ||
export const STALE_TEST_THRESHOLD_MS = 10000 |
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,18 @@ | ||
import {SanityDocument, SanityDocumentStub} from '@sanity/client' | ||
import {uuid} from '@sanity/uuid' | ||
import {testSanityClient} from '.' | ||
|
||
export async function createUniqueDocument({ | ||
_type, | ||
_id, | ||
...restProps | ||
}: SanityDocumentStub): Promise<Partial<SanityDocument>> { | ||
const doc = { | ||
_type, | ||
_id: _id || uuid(), | ||
...restProps, | ||
} | ||
|
||
await testSanityClient.create(doc) | ||
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './sanityClient' | ||
export * from './createUniqueDocument' | ||
export * from './constants' |
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,21 @@ | ||
import sanityClient from '@sanity/client' | ||
import {STALE_TEST_THRESHOLD_MS} from './constants' | ||
|
||
export const testSanityClient = sanityClient({ | ||
projectId: 'ppsg7ml5', | ||
dataset: 'test', | ||
token: process.env.PLAYWRIGHT_SANITY_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}, | ||
} | ||
} |
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,121 @@ | ||
import {test, expect} from '@playwright/test' | ||
import {uuid} from '@sanity/uuid' | ||
import { | ||
createUniqueDocument, | ||
deleteDocumentsForRun, | ||
STUDIO_SPACE_NAME, | ||
testSanityClient, | ||
} from '../../helpers' | ||
|
||
/* | ||
**NOTE** | ||
In these tests, we are not testing input events, so to speed up the tests we will use use `page.fill()` instead of `page.type()` to populate the document title field | ||
*/ | ||
|
||
const TITLE_PREFIX = '[playwright]' | ||
const inputSelector = 'data-testid=input-title >> input' | ||
const TEST_RUN_ID = uuid() | ||
|
||
// The path of the network request we want to wait for | ||
const documentActionPath = (action) => | ||
`*/**/mutate/${STUDIO_SPACE_NAME}?tag=sanity.studio.document.${action}*` | ||
|
||
// Helper to generate path to document created in a test | ||
const generateDocumentUri = (docId) => | ||
`/${STUDIO_SPACE_NAME}/desk/input-ci;documentActionsCI;${docId}` | ||
|
||
// Helper to get the path to the document the client creates for a test | ||
async function createDocument( | ||
_type: string, | ||
title?: string | ||
): Promise<{studioPath: string; documentId: string}> { | ||
const testDoc = await createUniqueDocument({runId: TEST_RUN_ID, _type, title}) | ||
return {studioPath: generateDocumentUri(testDoc?._id), documentId: testDoc?._id} | ||
} | ||
|
||
test.describe('@sanity/desk-tool: document actions', () => { | ||
test.afterEach(async ({page}, testInfo) => { | ||
// After each test, delete the created documents if the test passed | ||
if (testInfo.status === testInfo.expectedStatus) { | ||
await testSanityClient.delete(deleteDocumentsForRun('documentActionsCI', TEST_RUN_ID)) | ||
} | ||
}) | ||
|
||
test('edit and publish document', async ({page}, testInfo) => { | ||
const title = `${TITLE_PREFIX} ${testInfo.title}` | ||
const publishButton = page.locator('data-testid=action-Publish') | ||
const {studioPath} = await createDocument('documentActionsCI') | ||
await page.goto(studioPath) | ||
await page.fill(inputSelector, title) | ||
expect(page.locator(inputSelector)).toHaveValue(title) | ||
expect(publishButton).toBeEnabled() | ||
expect(publishButton).toHaveAttribute('data-disabled', 'false') | ||
|
||
await Promise.all([page.waitForResponse(documentActionPath('publish')), publishButton.click()]) | ||
await expect(publishButton).toBeDisabled() | ||
await expect(publishButton).toHaveAttribute('data-disabled', 'true') | ||
}) | ||
|
||
test('unpublish document', async ({page}, testInfo) => { | ||
const title = `${TITLE_PREFIX} ${testInfo.title}` | ||
const publishButton = page.locator('data-testid=action-Publish') | ||
|
||
const {studioPath} = await createDocument('documentActionsCI', title) | ||
|
||
await page.goto(studioPath) | ||
|
||
expect(page.locator(inputSelector)).toHaveValue(title) | ||
await expect(publishButton).toBeDisabled() | ||
expect(publishButton).toHaveAttribute('data-disabled', 'true') | ||
page.locator('data-testid=action-menu-button').click() | ||
page.locator('data-testid=action-Unpublish').click() | ||
|
||
await Promise.all([ | ||
page.waitForResponse(documentActionPath('unpublish')), | ||
page.locator('data-testid=confirm-unpublish').click(), | ||
]) | ||
|
||
await expect(publishButton).toBeEnabled() | ||
await expect(publishButton).toHaveAttribute('data-disabled', 'false') | ||
}) | ||
|
||
test('duplicate document', async ({page}, testInfo) => { | ||
const title = `${TITLE_PREFIX} ${testInfo.title}` | ||
const {studioPath} = await createDocument('documentActionsCI', title) | ||
|
||
await page.goto(studioPath) | ||
|
||
expect(page.locator(inputSelector)).toHaveValue(title) | ||
page.locator('data-testid=action-menu-button').click() | ||
|
||
await Promise.all([ | ||
page.waitForResponse(documentActionPath('duplicate')), | ||
page.locator('data-testid=action-Duplicate').click(), | ||
]) | ||
// Check if we are viewing a new document and not the one created for this test | ||
await expect(page.url()).not.toMatch(`*${studioPath}`) | ||
}) | ||
|
||
test('delete document', async ({page}, testInfo) => { | ||
const title = `${TITLE_PREFIX} ${testInfo.title}` | ||
const publishButton = page.locator('data-testid=action-Publish') | ||
|
||
const {studioPath, documentId} = await createDocument('documentActionsCI', title) | ||
|
||
await page.goto(studioPath) | ||
|
||
expect(page.locator(inputSelector)).toHaveValue(title) | ||
page.locator('data-testid=action-menu-button').click() | ||
page.locator('data-testid=action-Delete').click() | ||
|
||
await Promise.all([ | ||
page.waitForResponse(documentActionPath('delete')), | ||
page.locator('data-testid=confirm-delete').click(), | ||
]) | ||
await expect(publishButton).toBeDisabled() | ||
await expect(publishButton).toHaveAttribute('data-disabled', 'true') | ||
expect( | ||
await page.locator(`data-testid=pane-content >> data-testid=pane-item-${documentId}`).count() | ||
).toBe(0) | ||
}) | ||
}) |