File size: 3,911 Bytes
aec3094 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | import { mock } from 'jest-mock-extended';
import type { NpsSurveyState } from 'n8n-workflow';
import { UserSettingsController } from '@/controllers/user-settings.controller';
import type { NpsSurveyRequest } from '@/requests';
import type { UserService } from '@/services/user.service';
const NOW = 1717607016208;
jest.useFakeTimers({
now: NOW,
});
describe('UserSettingsController', () => {
const userService = mock<UserService>();
const controller = new UserSettingsController(userService);
describe('NPS Survey', () => {
test.each([
[
'updates user settings, setting response state to done',
{
responded: true,
lastShownAt: 1717607016208,
},
[],
],
[
'updates user settings, setting response state to done, ignoring other keys like waitForResponse',
{
responded: true,
lastShownAt: 1717607016208,
waitingForResponse: true,
},
['waitingForResponse'],
],
[
'updates user settings, setting response state to done, ignoring other keys like ignoredCount',
{
responded: true,
lastShownAt: 1717607016208,
ignoredCount: 1,
},
['ignoredCount'],
],
[
'updates user settings, setting response state to done, ignoring other unknown keys',
{
responded: true,
lastShownAt: 1717607016208,
x: 1,
},
['x'],
],
[
'updates user settings, updating ignore count',
{
waitingForResponse: true,
lastShownAt: 1717607016208,
ignoredCount: 1,
},
[],
],
[
'updates user settings, resetting to waiting state',
{
waitingForResponse: true,
ignoredCount: 0,
lastShownAt: 1717607016208,
},
[],
],
[
'updates user settings, updating ignore count, ignoring unknown keys',
{
waitingForResponse: true,
lastShownAt: 1717607016208,
ignoredCount: 1,
x: 1,
},
['x'],
],
])('%s', async (_, toUpdate, toIgnore: string[] | undefined) => {
const req = mock<NpsSurveyRequest.NpsSurveyUpdate>();
req.user.id = '1';
req.body = toUpdate;
await controller.updateNpsSurvey(req);
const npsSurvey = Object.keys(toUpdate).reduce(
(accu, key) => {
if ((toIgnore ?? []).includes(key)) {
return accu;
}
accu[key] = (toUpdate as Record<string, unknown>)[key];
return accu;
},
{} as Record<string, unknown>,
);
expect(userService.updateSettings).toHaveBeenCalledWith('1', { npsSurvey });
});
it('updates user settings, setting response state to done', async () => {
const req = mock<NpsSurveyRequest.NpsSurveyUpdate>();
req.user.id = '1';
const npsSurvey: NpsSurveyState = {
responded: true,
lastShownAt: 1717607016208,
};
req.body = npsSurvey;
await controller.updateNpsSurvey(req);
expect(userService.updateSettings).toHaveBeenCalledWith('1', { npsSurvey });
});
it('updates user settings, updating ignore count', async () => {
const req = mock<NpsSurveyRequest.NpsSurveyUpdate>();
req.user.id = '1';
const npsSurvey: NpsSurveyState = {
waitingForResponse: true,
lastShownAt: 1717607016208,
ignoredCount: 1,
};
req.body = npsSurvey;
await controller.updateNpsSurvey(req);
expect(userService.updateSettings).toHaveBeenCalledWith('1', { npsSurvey });
});
test.each([
['is missing', {}],
['is undefined', undefined],
['is responded but missing lastShownAt', { responded: true }],
['is waitingForResponse but missing lastShownAt', { waitingForResponse: true }],
[
'is waitingForResponse but missing ignoredCount',
{ lastShownAt: 123, waitingForResponse: true },
],
])('throws error when request payload is %s', async (_, payload) => {
const req = mock<NpsSurveyRequest.NpsSurveyUpdate>();
req.user.id = '1';
req.body = payload;
await expect(controller.updateNpsSurvey(req)).rejects.toThrowError();
});
});
});
|