|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import { GIT_SHA } from '../../../../../lib/components/internal/environment'; |
| 5 | +import { checkMissingStyles } from '../../../../../lib/components/internal/hooks/use-base-component/styles-check'; |
| 6 | +import { metrics } from '../../../../../lib/components/internal/metrics'; |
| 7 | +import { idleWithDelay } from '../styles-check'; |
| 8 | + |
| 9 | +jest.mock('../../../../../lib/components/internal/environment', () => ({ |
| 10 | + ...jest.requireActual('../../../../../lib/components/internal/environment'), |
| 11 | + PACKAGE_VERSION: '3.0.0 (abc)', |
| 12 | + GIT_SHA: 'abc', |
| 13 | +})); |
| 14 | + |
| 15 | +afterEach(() => { |
| 16 | + jest.resetAllMocks(); |
| 17 | +}); |
| 18 | + |
| 19 | +describe('checkMissingStyles', () => { |
| 20 | + let consoleWarnSpy: jest.SpyInstance; |
| 21 | + let sendPanoramaMetricSpy: jest.SpyInstance; |
| 22 | + const style = document.createElement('style'); |
| 23 | + document.body.append(style); |
| 24 | + |
| 25 | + beforeEach(() => { |
| 26 | + style.textContent = ``; |
| 27 | + consoleWarnSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); |
| 28 | + sendPanoramaMetricSpy = jest.spyOn(metrics, 'sendOpsMetricObject').mockImplementation(() => {}); |
| 29 | + }); |
| 30 | + |
| 31 | + test('should pass the check if styles found', () => { |
| 32 | + // using :root does not work in JSDOM: https://github.com/jsdom/jsdom/issues/3563 |
| 33 | + style.textContent = ` |
| 34 | + body { |
| 35 | + --awsui-version-info-${GIT_SHA}: true; |
| 36 | + } |
| 37 | + `; |
| 38 | + checkMissingStyles(); |
| 39 | + expect(consoleWarnSpy).not.toHaveBeenCalled(); |
| 40 | + expect(sendPanoramaMetricSpy).not.toHaveBeenCalled(); |
| 41 | + }); |
| 42 | + |
| 43 | + test('should detect missing styles', () => { |
| 44 | + checkMissingStyles(); |
| 45 | + expect(consoleWarnSpy).toHaveBeenCalledWith( |
| 46 | + 'Missing Cloudscape CSS for theme "default", version "3.0.0 (abc)", and git sha "abc".' |
| 47 | + ); |
| 48 | + expect(sendPanoramaMetricSpy).toHaveBeenCalledWith('awsui-missing-css-asset', {}); |
| 49 | + }); |
| 50 | + |
| 51 | + test('should report missing styles if a different version found', () => { |
| 52 | + style.textContent = ` |
| 53 | + body { |
| 54 | + --awsui-version-info-c4d5e6: true; |
| 55 | + } |
| 56 | + `; |
| 57 | + checkMissingStyles(); |
| 58 | + expect(consoleWarnSpy).toHaveBeenCalledWith( |
| 59 | + 'Missing Cloudscape CSS for theme "default", version "3.0.0 (abc)", and git sha "abc".' |
| 60 | + ); |
| 61 | + expect(sendPanoramaMetricSpy).toHaveBeenCalledWith('awsui-missing-css-asset', {}); |
| 62 | + }); |
| 63 | +}); |
| 64 | + |
| 65 | +describe('idleWithDelay', () => { |
| 66 | + const delay = (time: number) => new Promise(resolve => setTimeout(resolve, time)); |
| 67 | + |
| 68 | + beforeEach(() => { |
| 69 | + // simulate requestIdleCallback for JSDOM |
| 70 | + globalThis.requestIdleCallback = cb => setTimeout(cb, 0); |
| 71 | + }); |
| 72 | + |
| 73 | + test('does nothing if requestIdleCallback not supported', async () => { |
| 74 | + // @ts-expect-error simulate missing API |
| 75 | + globalThis.requestIdleCallback = undefined; |
| 76 | + const cb = jest.fn(); |
| 77 | + expect(requestIdleCallback).toBe(undefined); |
| 78 | + idleWithDelay(cb); |
| 79 | + await delay(1100); |
| 80 | + expect(cb).not.toHaveBeenCalled(); |
| 81 | + }); |
| 82 | + |
| 83 | + test('runs callback after a delay', async () => { |
| 84 | + const cb = jest.fn(); |
| 85 | + idleWithDelay(cb); |
| 86 | + await delay(1100); |
| 87 | + expect(cb).toHaveBeenCalled(); |
| 88 | + }); |
| 89 | + |
| 90 | + test('delay can be aborted', async () => { |
| 91 | + const cb = jest.fn(); |
| 92 | + const abort = idleWithDelay(cb); |
| 93 | + await delay(500); |
| 94 | + abort!(); |
| 95 | + await delay(600); |
| 96 | + expect(cb).not.toHaveBeenCalled(); |
| 97 | + }); |
| 98 | +}); |
0 commit comments