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