]> git.r.bdr.sh - rbdr/forum/blob - src/lib/components/topic/topic.test.ts
985ca72f0c0a5ab83f4b0e89533f96aa96575435
[rbdr/forum] / src / lib / components / topic / topic.test.ts
1 /**
2 * @jest-environment jsdom
3 */
4
5 import '@testing-library/jest-dom/extend-expect';
6
7 import { addMessages } from 'svelte-i18n';
8
9 import { cleanup, render } from '@testing-library/svelte';
10 import '$lib/i18n';
11
12 import Topic from './topic.svelte';
13
14 const internals = {
15 results: null,
16 basicTopic: {
17 id: 'b1a4f8d1-4d16-4872-b391-fda6a0e9012d',
18 title: 'I sure am a test topic',
19 ttl: 160 * 1000,
20 updated_at: Date.now(),
21 forum: {
22 id: 'diversion',
23 glyph: '⏃',
24 label: 'test_forums.diversion'
25 },
26 tags: [
27 {
28 id: 'fish',
29 weight: 40
30 },
31 {
32 id: 'statue',
33 weight: 5
34 }
35 ],
36 posts: [
37 {
38 id: '413a74db-9473-4bac-8698-da9452c05854',
39 text: 'This is the first post',
40 created_at: Date.UTC(1999, 7, 1, 8, 8, 2, 111).valueOf(),
41 author: {
42 handle: 'past_user',
43 id: 'c76d3e51-76ac-4e84-a1b2-2eee9abd68b3'
44 }
45 },
46 {
47 id: '821ff177-5250-406f-9431-1a8097b35430',
48 text: 'This response came later',
49 created_at: Date.UTC(2038, 1, 2, 3, 4, 6, 789).valueOf(),
50 author: {
51 handle: 'future_user',
52 id: 'cb9307cb-77e9-4c55-bbe7-dbbf88737358'
53 }
54 }
55 ]
56 },
57 topicWithoutForum: {
58 id: '9715e9ee-0d63-4b50-b613-826ef2791728',
59 title: 'This topic, no forums',
60 ttl: 160 * 1000,
61 updated_at: Date.now(),
62 tags: [
63 {
64 id: 'cauliflower',
65 weight: 33
66 }
67 ],
68 posts: []
69 }
70 };
71
72 describe('Topic component', () => {
73
74 beforeAll(() => {
75
76 addMessages('en', {
77 'test_forums.diversion': 'Diversion'
78 });
79 });
80
81 beforeEach(() => {
82
83 internals.results = render(Topic, { props: {
84 topic: internals.basicTopic
85 } });
86 });
87
88 test('Should show the topic title', () => {
89
90 expect(internals.results.getByText('I sure am a test topic'))
91 .toBeVisible();
92 });
93 test('Should display remaining time in readable format', () => {
94
95 expect(internals.results.getByText(/2 minutes remaining/))
96 .toBeVisible();
97 });
98 test('Remaining time should be a permalink to the topic', () => {
99
100 expect(internals.results.getByText(/2 minutes remaining/).closest('a'))
101 .toHaveAttribute('href', '/t/b1a4f8d1-4d16-4872-b391-fda6a0e9012d');
102 });
103
104 test('Should show text for all posts', () => {
105
106 expect(internals.results.getByText('This is the first post'))
107 .toBeVisible();
108 expect(internals.results.getByText('This response came later'))
109 .toBeVisible();
110 });
111
112 test('Should send index and count to posts', () => {
113
114 expect(internals.results.getByTitle('Post 1 of 2 by past_user'))
115 .toBeVisible();
116 expect(internals.results.getByTitle('Post 2 of 2 by future_user'))
117 .toBeVisible();
118 });
119
120 describe('Forum link', () => {
121
122 test('Should show forum if the post has one', () => {
123
124 expect(internals.results.getByText(/^\s*⏃\s*Diversion\s*$/))
125 .toBeVisible();
126 });
127
128 test('Forum text should be a permalink to the forum', () => {
129
130 expect(internals.results.getByText(/^\s*⏃\s*Diversion\s*$/).closest('a'))
131 .toHaveAttribute('href', '/f/diversion');
132 });
133
134 test('Should not show forum if the post doesn\'t have one', () => {
135
136 cleanup();
137 internals.results = render(Topic, { props: {
138 topic: internals.topicWithoutForum
139 } });
140
141 expect(internals.results.queryByText(/^\s*⏃\s*Diversion\s*$/))
142 .toBe(null);
143 });
144 });
145
146 describe('Tag listing', () => {
147
148 test('Should show topic tags', () => {
149
150 expect(internals.results.getByText('fish'))
151 .toBeVisible();
152 expect(internals.results.getByText('fish'))
153 .toHaveTextContent('fish(40)');
154 expect(internals.results.getByText('statue'))
155 .toBeVisible();
156 expect(internals.results.getByText('statue'))
157 .toHaveTextContent('statue(5)');
158 });
159
160 test('Tag text should be a permalink to the tag', () => {
161
162 expect(internals.results.getByText('fish').closest('a'))
163 .toHaveAttribute('href', '/g/fish');
164 expect(internals.results.getByText('statue').closest('a'))
165 .toHaveAttribute('href', '/g/statue');
166 });
167 });
168 });