|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import React from 'react'; |
| 5 | +import { render } from '@testing-library/react'; |
| 6 | +import { getComponentsTree } from '../utils'; |
| 7 | +import { METADATA_ATTRIBUTE, activateAnalyticsMetadata } from '../attributes'; |
| 8 | +import { ComponentOne, ComponentTwo, ComponentThree } from './components'; |
| 9 | + |
| 10 | +describe('getComponentsTree', () => { |
| 11 | + describe('with active analytics metadata', () => { |
| 12 | + beforeAll(() => { |
| 13 | + activateAnalyticsMetadata(true); |
| 14 | + }); |
| 15 | + test('returns an empty array when input is null', () => { |
| 16 | + expect(getComponentsTree(null)).toEqual([]); |
| 17 | + }); |
| 18 | + test('skips metadata that does not refer to a component', () => { |
| 19 | + const { container } = render( |
| 20 | + <div id="outer-target"> |
| 21 | + <ComponentOne /> |
| 22 | + </div> |
| 23 | + ); |
| 24 | + const target = container.querySelector('#outer-target') as HTMLElement; |
| 25 | + expect(getComponentsTree(target)).toEqual([ |
| 26 | + { name: 'ComponentOne', label: 'component label', properties: { multi: 'true' }, children: [] }, |
| 27 | + ]); |
| 28 | + }); |
| 29 | + test('only includes components inside the specified element', () => { |
| 30 | + const { container } = render( |
| 31 | + <> |
| 32 | + <div id="outer-target-1"> |
| 33 | + <ComponentOne /> |
| 34 | + </div> |
| 35 | + <div id="outer-target-2"> |
| 36 | + <ComponentTwo /> |
| 37 | + </div> |
| 38 | + </> |
| 39 | + ); |
| 40 | + expect(getComponentsTree(container.querySelector('#outer-target-1') as HTMLElement)).toEqual([ |
| 41 | + { name: 'ComponentOne', label: 'component label', properties: { multi: 'true' }, children: [] }, |
| 42 | + ]); |
| 43 | + expect(getComponentsTree(container.querySelector('#outer-target-2') as HTMLElement)).toEqual([ |
| 44 | + { name: 'ComponentTwo', label: 'sub label', children: [] }, |
| 45 | + ]); |
| 46 | + }); |
| 47 | + test('can include multiple components', () => { |
| 48 | + const { container } = render( |
| 49 | + <div id="outer-target-1"> |
| 50 | + <ComponentThree /> |
| 51 | + <ComponentTwo /> |
| 52 | + </div> |
| 53 | + ); |
| 54 | + expect(getComponentsTree(container.querySelector('#outer-target-1') as HTMLElement)).toEqual([ |
| 55 | + { |
| 56 | + name: 'ComponentThree', |
| 57 | + children: [ |
| 58 | + { name: 'ComponentTwo', label: 'sub label', children: [] }, |
| 59 | + { name: 'ComponentOne', label: 'component label', properties: { multi: 'true' }, children: [] }, |
| 60 | + ], |
| 61 | + }, |
| 62 | + { name: 'ComponentTwo', label: 'sub label', children: [] }, |
| 63 | + ]); |
| 64 | + }); |
| 65 | + test('can include multiple nested components', () => { |
| 66 | + const { container } = render( |
| 67 | + <div id="outer-target-1"> |
| 68 | + <ComponentThree> |
| 69 | + <ComponentThree /> |
| 70 | + </ComponentThree> |
| 71 | + </div> |
| 72 | + ); |
| 73 | + expect(getComponentsTree(container.querySelector('#outer-target-1') as HTMLElement)).toEqual([ |
| 74 | + { |
| 75 | + name: 'ComponentThree', |
| 76 | + children: [ |
| 77 | + { name: 'ComponentTwo', label: 'sub label', children: [] }, |
| 78 | + { name: 'ComponentOne', label: 'component label', properties: { multi: 'true' }, children: [] }, |
| 79 | + { |
| 80 | + name: 'ComponentThree', |
| 81 | + children: [ |
| 82 | + { name: 'ComponentTwo', label: 'sub label', children: [] }, |
| 83 | + { name: 'ComponentOne', label: 'component label', properties: { multi: 'true' }, children: [] }, |
| 84 | + ], |
| 85 | + }, |
| 86 | + ], |
| 87 | + }, |
| 88 | + ]); |
| 89 | + }); |
| 90 | + test('use document as default element', () => { |
| 91 | + render( |
| 92 | + <> |
| 93 | + <ComponentThree /> |
| 94 | + <ComponentTwo /> |
| 95 | + </> |
| 96 | + ); |
| 97 | + expect(getComponentsTree()).toEqual([ |
| 98 | + { |
| 99 | + name: 'ComponentThree', |
| 100 | + children: [ |
| 101 | + { name: 'ComponentTwo', label: 'sub label', children: [] }, |
| 102 | + { name: 'ComponentOne', label: 'component label', properties: { multi: 'true' }, children: [] }, |
| 103 | + ], |
| 104 | + }, |
| 105 | + { name: 'ComponentTwo', label: 'sub label', children: [] }, |
| 106 | + ]); |
| 107 | + }); |
| 108 | + test('skips malformed metadata', () => { |
| 109 | + const { container } = render( |
| 110 | + <div id="outer-target" {...{ [METADATA_ATTRIBUTE]: "{'corruptedJSON':}" }}> |
| 111 | + <ComponentOne malformed={true} /> |
| 112 | + </div> |
| 113 | + ); |
| 114 | + const target = container.querySelector('#outer-target') as HTMLElement; |
| 115 | + expect(getComponentsTree(target)).toEqual([ |
| 116 | + { name: 'ComponentOne', label: 'component label', properties: { multi: 'true' }, children: [] }, |
| 117 | + ]); |
| 118 | + }); |
| 119 | + }); |
| 120 | + |
| 121 | + describe('with inactive analytics metadata', () => { |
| 122 | + beforeAll(() => { |
| 123 | + activateAnalyticsMetadata(false); |
| 124 | + }); |
| 125 | + test('returns an empty object', () => { |
| 126 | + const { container } = render(<ComponentThree />); |
| 127 | + const target = container.querySelector('#target') as HTMLElement; |
| 128 | + expect(getComponentsTree(target)).toEqual([]); |
| 129 | + }); |
| 130 | + }); |
| 131 | +}); |
0 commit comments