Skip to content

Commit

Permalink
feat: optimize unit test
Browse files Browse the repository at this point in the history
Signed-off-by: SuZhou-Joe <[email protected]>
  • Loading branch information
SuZhou-Joe committed Oct 17, 2023
1 parent e80221e commit 46238e1
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
28 changes: 21 additions & 7 deletions src/plugins/workspace/server/integration_tests/routes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
*/

import { WorkspaceAttribute } from 'src/core/types';
import { omit } from 'lodash';
import * as osdTestServer from '../../../../core/test_helpers/osd_server';

const omitId = <T extends { id?: string }>(object: T): Omit<T, 'id'> => {
const { id, ...others } = object;
return others;
};

const testWorkspace: WorkspaceAttribute = {
id: 'fake_id',
name: 'test_workspace',
Expand Down Expand Up @@ -60,7 +64,7 @@ describe('workspace service', () => {
const result: any = await osdTestServer.request
.post(root, `/api/workspaces`)
.send({
attributes: omit(testWorkspace, 'id'),
attributes: omitId(testWorkspace),
})
.expect(200);

Expand All @@ -71,7 +75,7 @@ describe('workspace service', () => {
const result = await osdTestServer.request
.post(root, `/api/workspaces`)
.send({
attributes: omit(testWorkspace, 'id'),
attributes: omitId(testWorkspace),
})
.expect(200);

Expand All @@ -85,15 +89,15 @@ describe('workspace service', () => {
const result: any = await osdTestServer.request
.post(root, `/api/workspaces`)
.send({
attributes: omit(testWorkspace, 'id'),
attributes: omitId(testWorkspace),
})
.expect(200);

await osdTestServer.request
.put(root, `/api/workspaces/${result.body.result.id}`)
.send({
attributes: {
...omit(testWorkspace, 'id'),
...omitId(testWorkspace),
name: 'updated',
},
})
Expand All @@ -111,7 +115,7 @@ describe('workspace service', () => {
const result: any = await osdTestServer.request
.post(root, `/api/workspaces`)
.send({
attributes: omit(testWorkspace, 'id'),
attributes: omitId(testWorkspace),
})
.expect(200);

Expand All @@ -130,7 +134,17 @@ describe('workspace service', () => {
await osdTestServer.request
.post(root, `/api/workspaces`)
.send({
attributes: omit(testWorkspace, 'id'),
attributes: omitId(testWorkspace),
})
.expect(200);

await osdTestServer.request
.post(root, `/api/workspaces`)
.send({
attributes: {
...omitId(testWorkspace),
name: 'another test workspace',
},
})
.expect(200);

Expand Down
15 changes: 13 additions & 2 deletions test/api_integration/apis/workspace/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,25 @@ export default function ({ getService }: FtrProviderContext) {
.set('osd-xsrf', 'opensearch-dashboards')
.expect(200);

await supertest
.post(`/api/workspaces`)
.send({
attributes: {
...omitId(testWorkspace),
name: 'another test workspace',
},
})
.set('osd-xsrf', 'opensearch-dashboards')
.expect(200);

const listResult = await supertest
.post(`/api/workspaces/_list`)
.send({
page: 1,
})
.set('osd-xsrf', 'opensearch-dashboards')
.expect(200);
expect(listResult.body.result.total).equal(1);
expect(listResult.body.result.total).equal(2);
});
}).tags('is:workspace');
});
}

0 comments on commit 46238e1

Please sign in to comment.