]> git.r.bdr.sh - rbdr/forum/blob - src/components/post/post.test.js
Add gitlab CI config
[rbdr/forum] / src / components / post / post.test.js
1 import '@testing-library/jest-dom/extend-expect';
2
3 import { cleanup, render } from '@testing-library/svelte';
4 import '$/config/i18n';
5
6 import Post from './post.svelte';
7
8 const internals = {
9 basicPost: {
10 id: 'e5a19d53-4c9a-4be8-afa5-00942ea3afa4',
11 text: 'This is an example post qwerty',
12 created_at: Date.UTC(2021, 3, 19, 6, 6, 6, 666).valueOf(),
13 author: {
14 handle: 'very_cool_user',
15 id: 'b01bdb48-4b5e-46a4-97f3-6db789bcd33b'
16 },
17 topic: {
18 id: '35d3c3eb-e486-42ef-994c-d8ab1f1e167a',
19 title: 'Parent topic, yes'
20 }
21 },
22 postWithoutTopic: {
23 id: '9e52e38e-9007-4a20-bbf1-cea4e2f950f3',
24 text: 'This is a post without a topic',
25 created_at: Date.UTC(2022, 8, 21, 4, 3, 1, 340).valueOf(),
26 author: {
27 handle: 'my_normal_user',
28 id: '121f8f97-de02-4102-b25d-f34fd619009b'
29 }
30 },
31
32 results: null
33 };
34
35 describe('Post component', () => {
36
37 beforeEach(() => {
38
39 internals.results = render(Post, { props: {
40 post: internals.basicPost
41 } });
42 });
43
44 test('Should display the text of the post', () => {
45
46 expect(internals.results.getByText('This is an example post qwerty')).toBeVisible();
47 });
48
49 test('Should display date of the post', () => {
50
51 expect(internals.results.getByText('2021-04-19T06:06:06.666Z'))
52 .toBeVisible();
53 });
54
55 test('Date of post should be a permalink to the post', () => {
56
57 expect(internals.results.getByText('2021-04-19T06:06:06.666Z').closest('a'))
58 .toHaveAttribute('href', '/p/e5a19d53-4c9a-4be8-afa5-00942ea3afa4');
59 });
60
61 test('Should display the glyph of the post author', () => {
62
63 const glyphicon = internals.results.getByRole('img');
64
65 expect(glyphicon)
66 .toBeVisible();
67 expect(glyphicon)
68 .toHaveTextContent(/^. . . .$/);
69 });
70
71 test('Should display author handle', () => {
72
73 expect(internals.results.getByText('very_cool_user'))
74 .toBeVisible();
75 });
76
77 test('Author handle should have a permalink to topic', () => {
78
79 expect(internals.results.getByText('very_cool_user').closest('a'))
80 .toHaveAttribute('href', '/a/very_cool_user');
81 });
82
83 test('Should display parent topic title', () => {
84
85 expect(internals.results.getByText('Parent topic, yes'))
86 .toBeVisible();
87 });
88
89 test('Parent topic title should have a permalink to topic', () => {
90
91 expect(internals.results.getByText('Parent topic, yes').closest('a'))
92 .toHaveAttribute('href', '/t/35d3c3eb-e486-42ef-994c-d8ab1f1e167a');
93 });
94
95 test('Parent topic title should have a permalink to topic', () => {
96
97 cleanup();
98 internals.results = render(Post, { props: {
99 post: internals.postWithoutTopic
100 } });
101
102 expect(internals.results.queryByText('Parent topic, yes'))
103 .toBe(null);
104 });
105
106 test('It should default to 1/1 when no index or count is passed', () => {
107
108 expect(internals.results.getByTitle('Post 1 of 1 by very_cool_user'))
109 .toBeVisible();
110 });
111
112 test('Parent topic title should have a permalink to topic', () => {
113
114 cleanup();
115 internals.results = render(Post, { props: {
116 index: 2,
117 count: 5,
118 post: internals.postWithoutTopic
119 } });
120
121 expect(internals.results.getByTitle('Post 3 of 5 by my_normal_user'))
122 .toBeVisible();
123 });
124 });