-
Notifications
You must be signed in to change notification settings - Fork 737
Expand file tree
/
Copy pathDetectContext.spec.tsx
More file actions
102 lines (84 loc) · 3.22 KB
/
Copy pathDetectContext.spec.tsx
File metadata and controls
102 lines (84 loc) · 3.22 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
import { render, screen } from '@testing-library/react';
import { useLocation } from 'react-router';
import { usePerspectives } from '@console/shared/src/hooks/usePerspectives';
import { DetectContext } from '../DetectContext';
import { useValuesForPerspectiveContext } from '../useValuesForPerspectiveContext';
const MockApp = () => <h1>App</h1>;
jest.mock('../PerspectiveDetector', () => ({
__esModule: true,
default: () => 'PerspectiveDetector',
}));
jest.mock('../useValuesForPerspectiveContext', () => ({
useValuesForPerspectiveContext: jest.fn(),
}));
jest.mock('../namespace', () => ({
NamespaceContext: { Provider: ({ children }) => children, Consumer: () => null },
useValuesForNamespaceContext: jest.fn().mockReturnValue({
namespace: 'default',
setNamespace: jest.fn(),
loaded: true,
}),
}));
jest.mock('../../user-preferences/language/usePreferredLanguage', () => ({
usePreferredLanguage: jest.fn().mockReturnValue(['en', jest.fn(), true]),
}));
jest.mock('../../user-preferences/language/useLanguage', () => ({
useLanguage: jest.fn(),
}));
jest.mock('@console/shared/src/hooks/usePerspectives', () => ({
usePerspectives: jest.fn(),
}));
jest.mock('@console/dynamic-plugin-sdk', () => ({
PerspectiveContext: { Provider: ({ children }) => children, Consumer: () => null },
useResolvedExtensions: jest.fn().mockReturnValue([[], true]),
isContextProvider: jest.fn(),
isReduxReducer: jest.fn(),
}));
jest.mock('@console/internal/redux', () => ({
applyReduxExtensions: jest.fn(),
}));
jest.mock('react-router', () => ({
useLocation: jest.fn(),
useNavigate: jest.fn(() => jest.fn()),
createPath: jest.fn((loc) => `${loc.pathname}${loc.search || ''}${loc.hash || ''}`),
}));
const useValuesForPerspectiveContextMock = useValuesForPerspectiveContext as jest.Mock;
const usePerspectivesMock = usePerspectives as jest.Mock;
const useLocationMock = useLocation as jest.Mock;
describe('DetectContext', () => {
beforeEach(() => {
useValuesForPerspectiveContextMock.mockClear();
usePerspectivesMock.mockClear();
useLocationMock.mockClear();
useLocationMock.mockReturnValue({ pathname: '/test' });
});
it('should render children when there is an active perspective', () => {
useValuesForPerspectiveContextMock.mockReturnValue(['dev', jest.fn(), true]);
usePerspectivesMock.mockReturnValue([
{ properties: { id: 'admin' } },
{ properties: { id: 'dev' } },
{ properties: { id: 'dev-test' } },
]);
render(
<DetectContext>
<MockApp />
</DetectContext>,
);
expect(screen.getByRole('heading', { name: 'App' })).toBeVisible();
expect(screen.queryByText('PerspectiveDetector')).not.toBeInTheDocument();
});
it('should render PerspectiveDetector when there is no active perspective', () => {
useValuesForPerspectiveContextMock.mockReturnValue([undefined, jest.fn(), true]);
usePerspectivesMock.mockReturnValue([
{ properties: { id: 'admin' } },
{ properties: { id: 'dev' } },
]);
render(
<DetectContext>
<MockApp />
</DetectContext>,
);
expect(screen.getByText('PerspectiveDetector')).toBeVisible();
expect(screen.queryByRole('heading', { name: 'App' })).not.toBeInTheDocument();
});
});