|
| 1 | +import { IconType } from '@hypertrace/assets-library'; |
| 2 | +import { createHostFactory, SpectatorHost } from '@ngneat/spectator/jest'; |
| 3 | +import { MockComponent, MockDirective } from 'ng-mocks'; |
| 4 | +import { SummaryValueComponent, SummaryValueDisplayStyle } from '../summary-value/summary-value.component'; |
| 5 | +import { TooltipDirective } from '../tooltip/tooltip.directive'; |
| 6 | +import { SummaryValuesComponent } from './summary-values.component'; |
| 7 | + |
| 8 | +describe('Summary Values Component', () => { |
| 9 | + let spectator: SpectatorHost<SummaryValuesComponent>; |
| 10 | + |
| 11 | + const createHost = createHostFactory({ |
| 12 | + component: SummaryValuesComponent, |
| 13 | + shallow: true, |
| 14 | + declarations: [MockComponent(SummaryValueComponent), MockDirective(TooltipDirective)] |
| 15 | + }); |
| 16 | + |
| 17 | + test('should pass on inputs correct to child component when multiple values are present', () => { |
| 18 | + spectator = createHost( |
| 19 | + `<ht-summary-values [values]="values" [icon]="icon" [label]="label" [tooltip]="tooltip" [summaryValueDisplayStyle]="summaryValueDisplayStyle"> |
| 20 | + </ht-summary-values>`, |
| 21 | + { |
| 22 | + hostProps: { |
| 23 | + values: ['val1', 'val2', 'val3', 'val4', 'val5'], |
| 24 | + icon: IconType.Add, |
| 25 | + label: 'label', |
| 26 | + tooltip: 'label tooltip', |
| 27 | + summaryValueDisplayStyle: SummaryValueDisplayStyle.Text |
| 28 | + } |
| 29 | + } |
| 30 | + ); |
| 31 | + |
| 32 | + const summaryValueComponent = spectator.query(SummaryValueComponent); |
| 33 | + expect(summaryValueComponent).toExist(); |
| 34 | + expect(summaryValueComponent?.value).toEqual('val1'); |
| 35 | + expect(summaryValueComponent?.icon).toEqual(IconType.Add); |
| 36 | + expect(summaryValueComponent?.label).toEqual('label'); |
| 37 | + expect(summaryValueComponent?.tooltip).toEqual(undefined); |
| 38 | + expect(summaryValueComponent?.summaryValueDisplayStyle).toEqual(SummaryValueDisplayStyle.Text); |
| 39 | + |
| 40 | + expect(spectator.query('.additional-values')).toHaveText('+4'); |
| 41 | + expect(spectator.query(TooltipDirective)?.content).toBeDefined(); |
| 42 | + }); |
| 43 | + |
| 44 | + test('should hide additional details if single value is present', () => { |
| 45 | + spectator = createHost( |
| 46 | + `<ht-summary-values [values]="values" [icon]="icon" [label]="label" [tooltip]="tooltip" [summaryValueDisplayStyle]="summaryValueDisplayStyle"> |
| 47 | + </ht-summary-values>`, |
| 48 | + { |
| 49 | + hostProps: { |
| 50 | + values: ['val1'], |
| 51 | + icon: IconType.Add, |
| 52 | + label: 'label', |
| 53 | + tooltip: 'label tooltip', |
| 54 | + summaryValueDisplayStyle: SummaryValueDisplayStyle.Text |
| 55 | + } |
| 56 | + } |
| 57 | + ); |
| 58 | + |
| 59 | + const summaryValueComponent = spectator.query(SummaryValueComponent); |
| 60 | + expect(summaryValueComponent).toExist(); |
| 61 | + expect(summaryValueComponent?.value).toEqual('val1'); |
| 62 | + expect(summaryValueComponent?.icon).toEqual(IconType.Add); |
| 63 | + expect(summaryValueComponent?.label).toEqual('label'); |
| 64 | + expect(summaryValueComponent?.tooltip).toEqual(undefined); |
| 65 | + expect(summaryValueComponent?.summaryValueDisplayStyle).toEqual(SummaryValueDisplayStyle.Text); |
| 66 | + |
| 67 | + expect(spectator.query('.additional-values')).not.toExist(); |
| 68 | + expect(spectator.query(TooltipDirective)?.content).toBeDefined(); |
| 69 | + }); |
| 70 | + |
| 71 | + test('should hide additional details if empty array is passed as value', () => { |
| 72 | + spectator = createHost( |
| 73 | + `<ht-summary-values [values]="values" [icon]="icon" [label]="label" [tooltip]="tooltip" [summaryValueDisplayStyle]="summaryValueDisplayStyle"> |
| 74 | + </ht-summary-values>`, |
| 75 | + { |
| 76 | + hostProps: { |
| 77 | + values: [], |
| 78 | + icon: IconType.Add, |
| 79 | + label: 'label', |
| 80 | + tooltip: 'label tooltip', |
| 81 | + summaryValueDisplayStyle: SummaryValueDisplayStyle.Text |
| 82 | + } |
| 83 | + } |
| 84 | + ); |
| 85 | + |
| 86 | + const summaryValueComponent = spectator.query(SummaryValueComponent); |
| 87 | + expect(summaryValueComponent).toExist(); |
| 88 | + expect(summaryValueComponent?.value).not.toExist(); |
| 89 | + expect(summaryValueComponent?.icon).toEqual(IconType.Add); |
| 90 | + expect(summaryValueComponent?.label).toEqual('label'); |
| 91 | + expect(summaryValueComponent?.tooltip).toEqual(undefined); |
| 92 | + expect(summaryValueComponent?.summaryValueDisplayStyle).toEqual(SummaryValueDisplayStyle.Text); |
| 93 | + |
| 94 | + expect(spectator.query('.additional-values')).not.toExist(); |
| 95 | + expect(spectator.query(TooltipDirective)?.content).toBeDefined(); |
| 96 | + }); |
| 97 | + |
| 98 | + test('should handle empty strings', () => { |
| 99 | + spectator = createHost( |
| 100 | + `<ht-summary-values [values]="values" [icon]="icon" [label]="label" [tooltip]="tooltip" [summaryValueDisplayStyle]="summaryValueDisplayStyle"> |
| 101 | + </ht-summary-values>`, |
| 102 | + { |
| 103 | + hostProps: { |
| 104 | + values: ['', 'val2', 'val3', 'val4', 'val5'], |
| 105 | + icon: IconType.Add, |
| 106 | + label: 'label', |
| 107 | + tooltip: 'label tooltip', |
| 108 | + summaryValueDisplayStyle: SummaryValueDisplayStyle.Text |
| 109 | + } |
| 110 | + } |
| 111 | + ); |
| 112 | + |
| 113 | + const summaryValueComponent = spectator.query(SummaryValueComponent); |
| 114 | + expect(summaryValueComponent).toExist(); |
| 115 | + expect(summaryValueComponent?.value).toEqual('val2'); |
| 116 | + expect(summaryValueComponent?.icon).toEqual(IconType.Add); |
| 117 | + expect(summaryValueComponent?.label).toEqual('label'); |
| 118 | + expect(summaryValueComponent?.tooltip).toEqual(undefined); |
| 119 | + expect(summaryValueComponent?.summaryValueDisplayStyle).toEqual(SummaryValueDisplayStyle.Text); |
| 120 | + |
| 121 | + expect(spectator.query('.additional-values')).toHaveText('+3'); |
| 122 | + expect(spectator.query(TooltipDirective)?.content).toBeDefined(); |
| 123 | + }); |
| 124 | +}); |
0 commit comments