-
-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathOwnerMessage.test.jsx
More file actions
110 lines (89 loc) · 2.79 KB
/
OwnerMessage.test.jsx
File metadata and controls
110 lines (89 loc) · 2.79 KB
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
import { fireEvent, render, screen, waitFor } from '@testing-library/react';
import { Provider } from 'react-redux';
import { configureStore } from 'redux-mock-store';
import thunk from 'redux-thunk';
import OwnerMessage from '../OwnerMessage';
// Create mock for the hasPermission utility
vi.mock('utils/permissions', () => {
return vi.fn().mockImplementation(() => () => true);
});
describe('OwnerMessage Component', () => {
const middlewares = [thunk];
const mockStore = configureStore(middlewares);
const baseState = {
auth: {
user: {
role: 'Owner',
},
},
ownerMessage: {
message: 'Sample Message',
standardMessage: 'Sample Standard Message',
history: {
data: [],
pagination: {
page: 1,
totalPages: 1,
},
},
},
theme: {
darkMode: false,
},
};
function renderComponent(initialState = baseState) {
const store = mockStore(initialState);
store.dispatch = vi.fn();
render(
<Provider store={store}>
<OwnerMessage />
</Provider>,
);
return store;
}
it('should render without errors', () => {
renderComponent();
expect(screen.getByText('Sample Message')).toBeInTheDocument();
});
it('should display standard message when no owner message exists', () => {
renderComponent({
...baseState,
ownerMessage: {
...baseState.ownerMessage,
message: null,
},
});
expect(screen.getByText('Sample Standard Message')).toBeInTheDocument();
});
it('should show edit and delete buttons for owner role', () => {
renderComponent();
expect(screen.getByTitle('Edit this header')).toBeInTheDocument();
expect(screen.getByTitle('Click to restore header to standard message')).toBeInTheDocument();
});
it('hides the image upload controls after a photo is selected', async () => {
const originalFileReader = global.FileReader;
class MockFileReader {
onloadend = null;
readAsDataURL() {
this.result = 'data:image/png;base64,mock-image-payload';
queueMicrotask(() => {
this.onloadend?.();
});
}
}
global.FileReader = MockFileReader;
renderComponent();
fireEvent.click(screen.getByLabelText('Edit header message'));
expect(screen.getByText('Or upload a picture:')).toBeInTheDocument();
fireEvent.change(screen.getByLabelText('Choose owner message image'), {
target: {
files: [new File(['mock'], 'owner-message.png', { type: 'image/png' })],
},
});
await waitFor(() => {
expect(screen.queryByText('Or upload a picture:')).not.toBeInTheDocument();
});
expect(screen.queryByLabelText('Choose owner message image')).not.toBeInTheDocument();
global.FileReader = originalFileReader;
});
});