|
| 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 | + |
| 8 | +jest.mock('../../../../../lib/components/internal/environment', () => ({ |
| 9 | + ...jest.requireActual('../../../../../lib/components/internal/environment'), |
| 10 | + PACKAGE_VERSION: '3.0.0 (abc)', |
| 11 | + GIT_SHA: 'abc', |
| 12 | +})); |
| 13 | + |
| 14 | +let consoleWarnSpy: jest.SpyInstance; |
| 15 | +let sendPanoramaMetricSpy: jest.SpyInstance; |
| 16 | + |
| 17 | +const style = document.createElement('style'); |
| 18 | +document.body.append(style); |
| 19 | + |
| 20 | +beforeEach(() => { |
| 21 | + style.textContent = ``; |
| 22 | + consoleWarnSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); |
| 23 | + sendPanoramaMetricSpy = jest.spyOn(metrics, 'sendOpsMetricObject').mockImplementation(() => {}); |
| 24 | +}); |
| 25 | + |
| 26 | +afterEach(() => { |
| 27 | + jest.resetAllMocks(); |
| 28 | +}); |
| 29 | + |
| 30 | +test('should pass the check if styles found', () => { |
| 31 | + // using :root does not work in JSDOM: https://github.com/jsdom/jsdom/issues/3563 |
| 32 | + style.textContent = ` |
| 33 | + body { |
| 34 | + --awsui-version-info-${GIT_SHA}: true; |
| 35 | + } |
| 36 | + `; |
| 37 | + checkMissingStyles(); |
| 38 | + expect(consoleWarnSpy).not.toHaveBeenCalled(); |
| 39 | + expect(sendPanoramaMetricSpy).not.toHaveBeenCalled(); |
| 40 | +}); |
| 41 | + |
| 42 | +test('should detect missing styles', () => { |
| 43 | + checkMissingStyles(); |
| 44 | + expect(consoleWarnSpy).toHaveBeenCalledWith( |
| 45 | + 'Missing Cloudscape CSS for theme "default", version "3.0.0 (abc)", and git sha "abc".' |
| 46 | + ); |
| 47 | + expect(sendPanoramaMetricSpy).toHaveBeenCalledWith('awsui-missing-css-asset', {}); |
| 48 | +}); |
| 49 | + |
| 50 | +test('should report missing styles if a different version found', () => { |
| 51 | + style.textContent = ` |
| 52 | + body { |
| 53 | + --awsui-version-info-c4d5e6: true; |
| 54 | + } |
| 55 | + `; |
| 56 | + checkMissingStyles(); |
| 57 | + expect(consoleWarnSpy).toHaveBeenCalledWith( |
| 58 | + 'Missing Cloudscape CSS for theme "default", version "3.0.0 (abc)", and git sha "abc".' |
| 59 | + ); |
| 60 | + expect(sendPanoramaMetricSpy).toHaveBeenCalledWith('awsui-missing-css-asset', {}); |
| 61 | +}); |
0 commit comments