Skip to content

Commit

Permalink
fix(ci/cd): Correct tests about structures identifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
annelhote committed Oct 2, 2024
1 parent a45b2f0 commit d56aff1
Showing 1 changed file with 40 additions and 27 deletions.
67 changes: 40 additions & 27 deletions src/api/structures/__tests__/identifiers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
} from '../../resources';

let authorization;
let id;
let resourceId;

const payload = {
Expand All @@ -28,20 +27,16 @@ beforeAll(async () => {
resourceId = body.id;
});

beforeEach(async () => {
afterEach(async () => {
// Delete all structures identifiers
const { body } = await global.superapp
.post(`/${resource}/${resourceId}/${subresource}`)
.set('Authorization', authorization)
.send(payload);
id = body.id;
});
.get(`/${resource}/${resourceId}/${subresource}`)
.set('Authorization', authorization);
const promises = body.data.map((identifier) => global.superapp
.delete(`/${resource}/${resourceId}/${subresource}/${identifier.id}`)
.set('Authorization', authorization));

afterEach(async () => {
if (id) {
await global.superapp
.delete(`/${resource}/${resourceId}/${subresource}/${id}`)
.set('Authorization', authorization);
}
await Promise.all(promises);
});

describe('API > structures > identifiers > create', () => {
Expand Down Expand Up @@ -100,13 +95,18 @@ describe('API > structures > identifiers > create', () => {

describe('API > structures > identifiers > update', () => {
it('should update an existing identifier', async () => {
const { body: { id } } = await global.superapp
.post(`/${resource}/${resourceId}/${subresource}`)
.set('Authorization', authorization)
.send(payload);

const type = 'wikidata';
const { body } = await global.superapp
const { body: updatedBody } = await global.superapp
.patch(`/${resource}/${resourceId}/${subresource}/${id}`)
.set('Authorization', authorization)
.send({ type })
.expect(200);
expect(body.type).toBe(type);
expect(updatedBody.type).toBe(type);
});

it('should throw bad request if id too short', async () => {
Expand All @@ -126,6 +126,11 @@ describe('API > structures > identifiers > update', () => {
});

it('should throw bad request with badly formatted payload', async () => {
const { body: { id } } = await global.superapp
.post(`/${resource}/${resourceId}/${subresource}`)
.set('Authorization', authorization)
.send(payload);

await global.superapp
.patch(`/${resource}/${resourceId}/${subresource}/${id}`)
.set('Authorization', authorization)
Expand All @@ -134,6 +139,11 @@ describe('API > structures > identifiers > update', () => {
});

it('should accept empty dates', async () => {
const { body: { id } } = await global.superapp
.post(`/${resource}/${resourceId}/${subresource}`)
.set('Authorization', authorization)
.send(payload);

await global.superapp
.patch(`/${resource}/${resourceId}/${subresource}/${id}`)
.set('Authorization', authorization)
Expand All @@ -145,13 +155,19 @@ describe('API > structures > identifiers > update', () => {
describe('API > structures > identifiers > read', () => {
it('should read existing identifier', async () => {
const { body } = await global.superapp
.post(`/${resource}/${resourceId}/${subresource}`)
.set('Authorization', authorization)
.send(payload);
const { id } = body;

const { body: readBody } = await global.superapp
.get(`/${resource}/${resourceId}/${subresource}/${id}`)
.set('Authorization', authorization)
.expect(200);
expect(body.type).toBe(payload.type);
expect(body.value).toBe(payload.value);
expect(body.active).toBeFalsy();
expect(body.createdBy.lastName).toBe('user');
expect(readBody.type).toBe(payload.type);
expect(readBody.value).toBe(payload.value);
expect(readBody.active).toBeFalsy();
expect(readBody.createdBy.lastName).toBe('user');
});

it('should throw bad request if id too short', async () => {
Expand Down Expand Up @@ -185,6 +201,11 @@ describe('API > structures > identifiers > delete', () => {
});

it('should delete existing identifier', async () => {
const { body: { id } } = await global.superapp
.post(`/${resource}/${resourceId}/${subresource}`)
.set('Authorization', authorization)
.send(payload);

await global.superapp
.delete(`/${resource}/${resourceId}/${subresource}/${id}`)
.set('Authorization', authorization)
Expand Down Expand Up @@ -226,14 +247,6 @@ describe('API > structures > identifiers > list', () => {
});
});

beforeEach(async () => {
if (id) {
await global.superapp
.delete(`/${resource}/${resourceId}/${subresource}/${id}`)
.set('Authorization', authorization);
}
});

it('should list', async () => {
const { body } = await global.superapp
.get(`/${resource}/${resourceId}/${subresource}`)
Expand Down

0 comments on commit d56aff1

Please sign in to comment.