-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.test.tsx
165 lines (157 loc) · 6.67 KB
/
App.test.tsx
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { screen, within, render } from '@testing-library/react'
import App from './App'
import { act } from 'react';
import userEvent from '@testing-library/user-event';
import Card from './Components/Card'
import '@testing-library/jest-dom';
const fs = require('fs').promises;
const wait = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));
describe('Components', () => {
let mockData: string;
let mockData2: string;
beforeEach(async () => {
const data = await fs.readFile('./src/mock.txt', 'utf-8');
mockData = data.toString();
const data2 = await fs.readFile('./src/mock2.txt', 'utf-8');
mockData2 = data2.toString();
jest.clearAllMocks();
global.fetch = jest.fn(() =>
Promise.resolve({
text: () => mockData,
})
) as jest.Mock;
});
afterEach(() => {
jest.restoreAllMocks();
});
test('Checks loading and cards + loading cards after scrolling', async () => {
render(<App />)
expect(screen.getByText(/Loading.../i)).toBeInTheDocument();
const spinner = screen.getByTestId('spinner');
expect(spinner).toBeInTheDocument();
await wait(1000);
const initialCards = screen.getAllByTestId('card');
expect(initialCards).toHaveLength(10);
window.scrollY = document.documentElement.scrollHeight;
act(() => {
window.dispatchEvent(new Event('scroll'));
});
await wait(1500);
const initialCards2 = screen.getAllByTestId('card');
expect(initialCards2).toHaveLength(20);
}, 10000)
test('Checks deleting element', async () => {
render(<App />)
await wait(2000);
const initialCards = screen.getAllByTestId('card');
expect(initialCards).toHaveLength(10);
const nameElement = within(initialCards[0]).getAllByRole('heading', { level: 4 })[0].textContent;
if (nameElement){
expect(screen.queryByText(new RegExp(nameElement, 'i'))).toBeInTheDocument();
}
else{
throw new Error('No text found in the document.');
}
const deletebtn = within(initialCards[0]).getByTestId('delete');
userEvent.click(deletebtn);
await wait(1000);
const initialCards2 = screen.getAllByTestId('card');
expect(initialCards2).toHaveLength(10);
expect(screen.queryByText(new RegExp(nameElement, 'i'))).not.toBeInTheDocument();
})
test('Checks changing sorting direction', async () => {
render(<App />)
await wait(1000);
const initialCards = screen.getAllByTestId('card');
expect(initialCards).toHaveLength(10);
(global.fetch as jest.Mock).mockImplementationOnce(() =>
new Promise((resolve) => {
setTimeout(() => {
resolve({
text: () => Promise.resolve(mockData2),
});
}, 1000);
})
);
const changedir = screen.getByTestId('changedirection');
expect(changedir).toBeInTheDocument();
userEvent.click(changedir);
await wait(100);
expect(screen.getByText(/Loading.../i)).toBeInTheDocument();
const spinner = screen.getByTestId('spinner');
expect(spinner).toBeInTheDocument();
await wait(1000);
const initialCards2 = screen.getAllByTestId('card');
expect(initialCards2).toHaveLength(10);
})
test('Checks changing element', async () => {
render(<App />)
await wait(1000);
const initialCards = screen.getAllByTestId('card');
expect(initialCards).toHaveLength(10);
const changebtn = within(initialCards[0]).getByTestId('change');
userEvent.click(changebtn);
await wait(100);
const name = within(initialCards[0]).getByTestId('inputname');
userEvent.type(name, 'Hello, World!');
await wait(250);
const genre = within(initialCards[0]).getByTestId('inputgenre');
userEvent.type(genre, 'Hello, World...');
await wait(250);
userEvent.click(changebtn);
await wait(100);
expect(screen.getByText(/Hello, World!/i)).toBeInTheDocument();
expect(screen.getByText(/Hello, World.../i)).toBeInTheDocument();
})
test('Checks changing sorting', async () => {
render(<App />)
await wait(1000);
const initialCards = screen.getAllByTestId('card');
expect(initialCards).toHaveLength(10);
const options = ['rating', 'alphabetical', 'popularity', 'ranking'];
for (let i = 0; i < options.length; i++) {
(global.fetch as jest.Mock).mockImplementationOnce(() =>
new Promise((resolve) => {
setTimeout(() => {
resolve({
text: () => Promise.resolve(( i % 2 == 0) ? mockData2: mockData),
});
}, 1000);
})
);
const dropdown = screen.getByText(new RegExp(options[i > 0 ? i-1 : options.length-1], 'i'));
userEvent.click(dropdown);
const option = await screen.findByTestId(options[i]);
userEvent.click(option);
await wait(100);
expect(screen.getByText(/Loading.../i)).toBeInTheDocument();
const spinner = screen.getByTestId('spinner');
expect(spinner).toBeInTheDocument();
await wait(1000);
const initialCards2 = screen.getAllByTestId('card');
expect(initialCards2).toHaveLength(10);
}
}, 10000)
test('Checks card', async () => {
render(<Card
name = "The Shawshank Redemption"
image = "https://m.media-amazon.com/images/M/MV5BMDAyY2FhYjctNDc5OS00MDNlLThiMGUtY2UxYWVkNGY2ZjljXkEyXkFqcGc@._V1_.jpg"
rating = {9.3}
contentRating='16+'
genre='Drama'
url='https://www.imdb.com/title/tt0111161/'
id={0}
/>)
expect(screen.getByText(/The Shawshank Redemption/i)).toBeInTheDocument();
const imageSrc = 'https://m.media-amazon.com/images/M/MV5BMDAyY2FhYjctNDc5OS00MDNlLThiMGUtY2UxYWVkNGY2ZjljXkEyXkFqcGc@._V1_.jpg';
const image = screen.getByAltText("The Shawshank Redemption");
expect(image).toBeInTheDocument();
expect(image).toHaveAttribute('src', imageSrc);
expect(screen.getByText(/9.3/i)).toBeInTheDocument();
expect(screen.getByText(/16+/i)).toBeInTheDocument();
expect(screen.getByText(/Drama/i)).toBeInTheDocument();
const url = screen.getByText(/IMDb/i);
const urlSrc = 'https://www.imdb.com/title/tt0111161/';
expect(url).toHaveAttribute('href', urlSrc);
})
})