refactor(e2e): shuold use test.extend to extend createCollections (T-2157)

This commit is contained in:
Rain 2023-10-10 08:20:22 +08:00
parent f85fb6d7ba
commit 9ee18ca2aa
2 changed files with 28 additions and 2 deletions

View File

@ -253,4 +253,12 @@ test.describe('createCollections', () => {
await expect(page.getByRole('menuitem', { name: 'collection1' })).toBeVisible();
await expect(page.getByRole('menuitem', { name: 'collection2' })).toBeVisible();
});
test('no page, just only create collections', async ({ page, createCollections }) => {
await createCollections(pageConfig.collections);
await page.goto('/admin/settings/collection-manager/collections');
await expect(page.getByText('collection1')).toBeVisible();
await expect(page.getByText('collection2')).toBeVisible();
});
});

View File

@ -165,7 +165,10 @@ class NocoPage {
}
}
export const test = base.extend<{ mockPage: (config?: PageConfig) => NocoPage }>({
export const test = base.extend<{
mockPage: (config?: PageConfig) => NocoPage;
createCollections: (collectionSettings: CollectionSetting | CollectionSetting[]) => Promise<void>;
}>({
mockPage: async ({ page }, use) => {
// 保证每个测试运行时 faker 的随机值都是一样的
faker.seed(1);
@ -184,6 +187,21 @@ export const test = base.extend<{ mockPage: (config?: PageConfig) => NocoPage }>
await nocoPage.destroy();
}
},
createCollections: async ({ page }, use) => {
let collectionsName = [];
const _createCollections = async (collectionSettings: CollectionSetting | CollectionSetting[]) => {
collectionSettings = Array.isArray(collectionSettings) ? collectionSettings : [collectionSettings];
collectionsName = collectionSettings.map((item) => item.name);
await createCollections(collectionSettings);
};
await use(_createCollections);
if (collectionsName.length) {
await deleteCollections(collectionsName);
}
},
});
const getStorageItem = (key: string, storageState: any) => {
@ -357,7 +375,7 @@ const deleteKeyOfCollection = (collectionSettings: CollectionSetting[]) => {
* @param collectionSettings
* @returns
*/
export const createCollections = async (collectionSettings: CollectionSetting | CollectionSetting[]) => {
const createCollections = async (collectionSettings: CollectionSetting | CollectionSetting[]) => {
const api = await request.newContext({
storageState: require.resolve('../../../../../playwright/.auth/admin.json'),
});