|
1 | 1 | // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
2 | 2 | // SPDX-License-Identifier: Apache-2.0
|
3 | 3 | import React, { useRef } from 'react';
|
4 |
| -import { useCurrentMode, useDensityMode, useVisualRefresh } from '../index'; |
| 4 | +import { useCurrentMode, useDensityMode, useVisualRefresh, clearVisualRefreshState } from '../index'; |
5 | 5 | import { render, screen } from '@testing-library/react';
|
6 | 6 | import { mutate } from './utils';
|
7 | 7 |
|
8 |
| -const originalFn = window.CSS.supports; |
9 |
| -beforeEach(() => { |
10 |
| - window.CSS.supports = jest.fn().mockReturnValue(true); |
11 |
| -}); |
12 |
| - |
13 |
| -afterEach(() => { |
14 |
| - window.CSS.supports = originalFn; |
15 |
| -}); |
16 |
| - |
17 | 8 | jest.mock('../../../environment', () => ({ ALWAYS_VISUAL_REFRESH: false }), { virtual: true });
|
18 | 9 |
|
19 | 10 | describe('useCurrentMode', () => {
|
@@ -83,11 +74,32 @@ describe('useVisualRefresh', () => {
|
83 | 74 | return <div data-testid="current-mode">{isRefresh.toString()}</div>;
|
84 | 75 | }
|
85 | 76 |
|
86 |
| - test('should return false when CSS-variables are not supported', () => { |
87 |
| - (window.CSS.supports as jest.Mock).mockReturnValue(false); |
| 77 | + beforeEach(() => clearVisualRefreshState()); |
| 78 | + afterEach(() => document.body.classList.remove('awsui-visual-refresh')); |
| 79 | + afterEach(() => jest.restoreAllMocks()); |
| 80 | + |
| 81 | + test('should return false when class name is not present', () => { |
88 | 82 | render(<App />);
|
89 | 83 | expect(screen.getByTestId('current-mode')).toHaveTextContent('false');
|
90 | 84 | });
|
| 85 | + |
| 86 | + test('should return true when class name is present', () => { |
| 87 | + document.body.classList.add('awsui-visual-refresh'); |
| 88 | + render(<App />); |
| 89 | + expect(screen.getByTestId('current-mode')).toHaveTextContent('true'); |
| 90 | + }); |
| 91 | + |
| 92 | + test('should print a warning when late visual refresh class name was detected', () => { |
| 93 | + jest.spyOn(console, 'warn').mockImplementation(() => {}); |
| 94 | + const { rerender } = render(<App />); |
| 95 | + expect(screen.getByTestId('current-mode')).toHaveTextContent('false'); |
| 96 | + expect(console.warn).not.toHaveBeenCalled(); |
| 97 | + |
| 98 | + document.body.classList.add('awsui-visual-refresh'); |
| 99 | + rerender(<App />); |
| 100 | + expect(console.warn).toHaveBeenCalledWith(expect.stringMatching(/Dynamic visual refresh change detected/)); |
| 101 | + expect(screen.getByTestId('current-mode')).toHaveTextContent('false'); |
| 102 | + }); |
91 | 103 | });
|
92 | 104 |
|
93 | 105 | // The above suites cover majority of cases
|
|
0 commit comments