Skip to content

Commit

Permalink
adds unit tests for reet level endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
pmarsh-scottlogic committed Mar 21, 2024
1 parent 71541ab commit 3740636
Showing 1 changed file with 100 additions and 1 deletion.
101 changes: 100 additions & 1 deletion backend/test/unit/controller/resetController.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { expect, test, jest, afterEach, describe } from '@jest/globals';
import { Response } from 'express';

import { handleResetProgress } from '@src/controller/resetController';
import {
handleResetLevel,
handleResetProgress,
} from '@src/controller/resetController';
import { defaultDefences } from '@src/defaultDefences';
import { LevelGetRequest } from '@src/models/api/LevelGetRequest';
import { LevelResetRequest } from '@src/models/api/LevelResetRequest';
import { defaultChatModel } from '@src/models/chat';
import { LEVEL_NAMES } from '@src/models/level';

Expand Down Expand Up @@ -158,3 +162,98 @@ describe('reset progress', () => {
expect(mockSend).toHaveBeenCalledWith('Invalid level');
});
});

describe('reset level', () => {
test.each(Object.values(LEVEL_NAMES))(
`GIVEN level [%s] WHEN client asks to reset level progress THEN sent messages and emails are cleared AND the backend sends an info message`,
(level) => {
const req = {
query: {
level,
},
session: {
levelState: [
{
sentEmails: 'level 1 emails',
chatHistory: 'level 1 chat history',
defences: 'level 1 defences',
},
{
sentEmails: 'level 2 emails',
chatHistory: 'level 2 chat history',
defences: 'level 2 defences',
},
{
sentEmails: 'level 3 emails',
chatHistory: 'level 3 chat history',
defences: 'level 3 defences',
},
{
sentEmails: 'level 4 emails',
chatHistory: 'level 4 chat history',
defences: 'level 4 defences',
},
],
chatModel: 'chat model',
},
} as unknown as LevelResetRequest;
const res = responseMock();

handleResetLevel(req, res);

expect(req.session.levelState[level].chatHistory).toEqual([]);
expect(req.session.levelState[level].sentEmails).toEqual([]);

expect(mockSend).toHaveBeenCalledWith({
resultingChatMessage: {
infoMessage: 'Level progress reset',
chatMessageType: 'RESET_LEVEL',
},
});
}
);

test('WHEN client does not provide a level THEN the backend responds with BadRequest', () => {
const req = {
query: {},
session: {
levelState: [
{},
{
sentEmails: [],
chatHistory: [],
defences: [],
},
],
},
} as unknown as LevelResetRequest;
const res = responseMock();

handleResetLevel(req, res);

expect(res.status).toHaveBeenCalledWith(400);
expect(mockSend).toHaveBeenCalledWith('Level not provided');
});

test('WHEN client provides an invalid level THEN the backend responds with BadRequest', () => {
const req = {
query: { level: 5 },
session: {
levelState: [
{},
{
sentEmails: [],
chatHistory: [],
defences: [],
},
],
},
} as unknown as LevelResetRequest;
const res = responseMock();

handleResetLevel(req, res);

expect(res.status).toHaveBeenCalledWith(400);
expect(mockSend).toHaveBeenCalledWith('Invalid level');
});
});

0 comments on commit 3740636

Please sign in to comment.