This repository was archived by the owner on Jan 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAccountMenu.test.tsx
95 lines (69 loc) · 2.64 KB
/
AccountMenu.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
import {act, fireEvent, render, screen} from '@testing-library/react';
import AccountMenu from '../app/[locale]/components/AccountMenu';
import React from "react";
import * as api from "@lib/api";
jest.mock('react-i18next', () => ({
useTranslation: () => ({t: (key: any) => key})
}));
jest.mock('../lib/api', () => ({
getUserData: jest.fn(() => Promise.resolve({first_name: 'First', last_name: 'Last'})),
APIError: function () {
this.message = '';
}
}));
describe('AccountMenu', () => {
it('name is displayed properly', async () => {
await act(async () => {
render(<AccountMenu/>);
});
// make sure the name is displayed correctly
expect(await screen.findByText('First Last')).toBeInTheDocument();
});
it('opens the menu when button is clicked', async () => {
await act(async () => {
render(<AccountMenu/>);
});
fireEvent.click(screen.getByRole('button'));
expect(screen.getByRole('menu')).toBeVisible();
});
it('closes the menu when an item is clicked', async () => {
await act(async () => {
render(<AccountMenu/>);
});
fireEvent.click(screen.getByRole('button'));
const menu = screen.getByRole('menu');
expect(menu).toBeVisible();
});
it('logs out when the logout button is clicked', async () => {
await act(async () => {
render(<AccountMenu/>);
});
const originalLocation = window.location;
delete window.location;
window.location = {...originalLocation, href: ''};
fireEvent.click(screen.getByRole('button'));
fireEvent.click(screen.getByRole('menuitem', {name: 'logout'}));
expect(window.location.href).not.toBe(originalLocation);
window.location = originalLocation;
});
it('goes to profile page when profile button clicked', async () => {
await act(async () => {
render(<AccountMenu/>);
});
const originalLocation = window.location;
delete window.location;
window.location = {...originalLocation, href: ''};
fireEvent.click(screen.getByRole('button'));
fireEvent.click(screen.getByRole('menuitem', {name: 'my_profile'}));
expect(window.location.href).toBe('/profile');
window.location = originalLocation;
});
it('test for API error', async () => {
const error = new api.APIError();
error.message = "Test error message";
(api.getUserData as jest.Mock).mockRejectedValueOnce(error);
await act(async () => {
render(<AccountMenu/>);
});
});
});