docbrown/app/javascript/chat/__tests__/util.test.js
Nick Taylor 24621b03f2 Restructure pack files part I (#377)
* Renamed pack file from chat.jsx to Chat.jsx.

* refactor

* Missed a js pack where chat.js -> Chat.jsx

* 👷Refactor

* 👷Refactor

* 👷Refactor

* 👷Refactor

* Tests

* Fixed some wording in a test description.
2018-08-29 12:59:20 -04:00

109 lines
2.7 KiB
JavaScript

import { getUserDataAndCsrfToken } from '../util';
const ERROR_MESSAGE = "Couldn't find user data on page.";
describe('Chat utilities', () => {
describe('getUserDataAndCsrfToken', () => {
afterEach(() => {
document.head.innerHTML = '';
document.body.removeAttribute('data-user');
});
test('should reject if no user or csrf token found.', async () => {
await expect(getUserDataAndCsrfToken(document)).rejects.toThrow(
ERROR_MESSAGE,
);
});
test('should reject if csrf token found but no user.', async () => {
document.head.innerHTML =
'<meta name="csrf-token" content="some-csrf-token" />';
await expect(getUserDataAndCsrfToken(document)).rejects.toThrow(
ERROR_MESSAGE,
);
});
test('should reject if user found but no csrf token found.', async () => {
document.body.setAttribute('data-user', '{}');
await expect(getUserDataAndCsrfToken(document)).rejects.toThrow(
ERROR_MESSAGE,
);
});
test('should resolve if user and csrf token found.', () => {
const csrfToken = 'some-csrf-token';
const currentUser = {
id: 41,
name: 'Guy Fieri',
username: 'guyfieri',
profile_image_90:
'/uploads/user/profile_image/41/0841dbe2-208c-4daa-b498-b2f01f3d37b2.png',
followed_tag_names: [],
followed_tags: '[]',
followed_user_ids: [
2,
31,
7,
38,
22,
37,
14,
26,
20,
24,
11,
27,
29,
3,
6,
28,
4,
39,
8,
40,
25,
30,
35,
34,
5,
12,
33,
36,
21,
18,
23,
1,
32,
19,
15,
13,
16,
9,
10,
17,
],
reading_list_ids: [48, 49, 34, 51, 64, 56],
saw_onboarding: true,
onboarding_checklist: {
write_your_first_article: false,
follow_your_first_tag: false,
fill_out_your_profile: false,
leave_your_first_reaction: true,
follow_your_first_dev: true,
leave_your_first_comment: false,
},
checked_code_of_conduct: false,
number_of_comments: 0,
display_sponsors: true,
trusted: false,
};
document.head.innerHTML = `<meta name="csrf-token" content="${csrfToken}" />`;
document.body.setAttribute('data-user', JSON.stringify(currentUser));
expect(getUserDataAndCsrfToken(document)).resolves.toEqual({
currentUser,
csrfToken,
});
});
});
});