-
Notifications
You must be signed in to change notification settings - Fork 11.6k
/
AngularDeprecationNotice.test.tsx
86 lines (70 loc) · 3.19 KB
/
AngularDeprecationNotice.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
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import React from 'react';
import { reportInteraction } from '@grafana/runtime';
import { AngularDeprecationNotice } from './AngularDeprecationNotice';
jest.mock('@grafana/runtime', () => ({
...jest.requireActual('@grafana/runtime'),
reportInteraction: jest.fn(),
}));
function localStorageKey(dsUid: string) {
return `grafana.angularDeprecation.dashboardNoticeAndMigration.isDismissed.${dsUid}`;
}
describe('AngularDeprecationNotice', () => {
const noticeText = /This dashboard depends on Angular/i;
const dsUid = 'abc';
afterAll(() => {
jest.resetAllMocks();
});
beforeEach(() => {
jest.clearAllMocks();
window.localStorage.clear();
});
it('should render', () => {
render(<AngularDeprecationNotice dashboardUid={dsUid} />);
expect(screen.getByText(noticeText)).toBeInTheDocument();
});
it('should be dismissable', async () => {
render(<AngularDeprecationNotice dashboardUid={dsUid} />);
const closeButton = screen.getByRole('button');
expect(closeButton).toBeInTheDocument();
await userEvent.click(closeButton);
expect(screen.queryByText(noticeText)).not.toBeInTheDocument();
});
it('should persist dismission status in localstorage', async () => {
render(<AngularDeprecationNotice dashboardUid={dsUid} />);
expect(window.localStorage.getItem(localStorageKey(dsUid))).toBeNull();
const closeButton = screen.getByRole('button');
expect(closeButton).toBeInTheDocument();
await userEvent.click(closeButton);
expect(window.localStorage.getItem(localStorageKey(dsUid))).toBe('true');
});
it('should not re-render alert if already dismissed', () => {
window.localStorage.setItem(localStorageKey(dsUid), 'true');
render(<AngularDeprecationNotice dashboardUid={dsUid} />);
expect(screen.queryByText(noticeText)).not.toBeInTheDocument();
});
it('should call reportInteraction when dismissing', async () => {
render(<AngularDeprecationNotice dashboardUid={dsUid} />);
const closeButton = screen.getByRole('button');
expect(closeButton).toBeInTheDocument();
await userEvent.click(closeButton);
expect(reportInteraction).toHaveBeenCalledWith('angular_deprecation_notice_dismissed');
});
describe('auto migrate button', () => {
const autoMigrateText = 'Try migration';
it('should display auto migrate button if showAutoMigrateLink is true', () => {
render(<AngularDeprecationNotice dashboardUid={dsUid} showAutoMigrateLink={true} />);
const autoMigrateButton = screen.getByRole('button', { name: /Try migration/i });
expect(autoMigrateButton).toBeInTheDocument();
});
it('should not display auto migrate button if showAutoMigrateLink is false', () => {
render(<AngularDeprecationNotice dashboardUid={dsUid} showAutoMigrateLink={false} />);
expect(screen.queryByText(autoMigrateText)).not.toBeInTheDocument();
});
it('should not display auto migrate link if showAutoMigrateLink is not provided', () => {
render(<AngularDeprecationNotice dashboardUid={dsUid} />);
expect(screen.queryByText(autoMigrateText)).not.toBeInTheDocument();
});
});
});