forked from processing/p5.js-web-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSketchList.unit.test.jsx
116 lines (94 loc) · 3.47 KB
/
SketchList.unit.test.jsx
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
import React from 'react';
import configureStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import { setupServer } from 'msw/node';
import { rest } from 'msw';
import { act } from 'react-dom/test-utils';
import SketchList from './SketchList';
import { reduxRender, fireEvent, screen, within } from '../../../test-utils';
import { initialTestState } from '../../../testData/testReduxStore';
const server = setupServer(
rest.get(`/${initialTestState.user.username}/projects`, (req, res, ctx) =>
// it just needs to return something so it doesn't throw an error
// Sketchlist tries to grab projects on creation but for the unit test
// we just feed those in as part of the initial state
res(ctx.json({ data: 'foo' }))
)
);
beforeAll(() => server.listen());
afterEach(() => server.resetHandlers());
afterAll(() => server.close());
describe('<Sketchlist />', () => {
const mockStore = configureStore([thunk]);
const store = mockStore(initialTestState);
let subjectProps = { username: initialTestState.user.username };
const subject = () =>
reduxRender(<SketchList {...subjectProps} />, { store });
afterEach(() => {
store.clearActions();
});
it('has sample projects', () => {
act(() => {
subject();
});
expect(screen.getByText('testsketch1')).toBeInTheDocument();
expect(screen.getByText('testsketch2')).toBeInTheDocument();
});
it('clicking on date created row header dispatches a reordering action', () => {
act(() => {
subject();
});
act(() => {
fireEvent.click(screen.getByText(/date created/i));
});
const expectedAction = [{ type: 'TOGGLE_DIRECTION', field: 'createdAt' }];
expect(store.getActions()).toEqual(expect.arrayContaining(expectedAction));
});
it('clicking on dropdown arrow opens sketch options - sketches belong to user', () => {
act(() => {
subject();
});
const row = screen.getByRole('row', {
name: /testsketch1/
});
const dropdown = within(row).getByRole('button', {
name: 'Toggle Open/Close Sketch Options'
});
act(() => {
fireEvent.click(dropdown);
});
expect(screen.queryByText('Rename')).toBeInTheDocument();
expect(screen.queryByText('Duplicate')).toBeInTheDocument();
expect(screen.queryByText('Download')).toBeInTheDocument();
expect(screen.queryByText('Add to collection')).toBeInTheDocument();
expect(screen.queryByText('Delete')).toBeInTheDocument();
});
it('snapshot testing', () => {
const { asFragment } = subject();
expect(asFragment()).toMatchSnapshot();
});
describe('different user than the one who created the sketches', () => {
beforeAll(() => {
subjectProps = { username: 'notthesameusername' };
});
it('clicking on dropdown arrow opens sketch options without Rename or Delete option', () => {
act(() => {
subject();
});
const row = screen.getByRole('row', {
name: /testsketch1/
});
const dropdown = within(row).getByRole('button', {
name: 'Toggle Open/Close Sketch Options'
});
act(() => {
fireEvent.click(dropdown);
});
expect(screen.queryByText('Rename')).not.toBeInTheDocument();
expect(screen.queryByText('Duplicate')).toBeInTheDocument();
expect(screen.queryByText('Download')).toBeInTheDocument();
expect(screen.queryByText('Add to collection')).toBeInTheDocument();
expect(screen.queryByText('Delete')).not.toBeInTheDocument();
});
});
});