|
| 1 | +import { ChangeDetectionStrategy, Component, input, model } from '@angular/core'; |
| 2 | +import { FormsModule } from '@angular/forms'; |
| 3 | +import { fireEvent, render } from '@testing-library/angular'; |
| 4 | +import { BrnToggleGroup } from './brn-toggle-group'; |
| 5 | +import { BrnToggleGroupItem } from './brn-toggle-item'; |
| 6 | + |
| 7 | +@Component({ |
| 8 | + imports: [BrnToggleGroupItem, BrnToggleGroup], |
| 9 | + template: ` |
| 10 | + <brn-toggle-group [(value)]="value" [disabled]="disabled()" [type]="type()"> |
| 11 | + <button brnToggleGroupItem value="option-1">Option 1</button> |
| 12 | + <button brnToggleGroupItem value="option-2">Option 2</button> |
| 13 | + <button brnToggleGroupItem value="option-3">Option 3</button> |
| 14 | + </brn-toggle-group> |
| 15 | + `, |
| 16 | + changeDetection: ChangeDetectionStrategy.OnPush, |
| 17 | +}) |
| 18 | +class BrnToggleGroupDirectiveSpec { |
| 19 | + public readonly value? = model<string | string[]>(); |
| 20 | + public readonly disabled = input(false); |
| 21 | + public readonly type = input('single'); |
| 22 | +} |
| 23 | + |
| 24 | +@Component({ |
| 25 | + imports: [BrnToggleGroupItem, BrnToggleGroup, FormsModule], |
| 26 | + template: ` |
| 27 | + <brn-toggle-group [(ngModel)]="value" [type]="type()"> |
| 28 | + <button brnToggleGroupItem value="option-1">Option 1</button> |
| 29 | + <button brnToggleGroupItem value="option-2">Option 2</button> |
| 30 | + <button brnToggleGroupItem value="option-3">Option 3</button> |
| 31 | + </brn-toggle-group> |
| 32 | + `, |
| 33 | + changeDetection: ChangeDetectionStrategy.OnPush, |
| 34 | +}) |
| 35 | +class BrnToggleGroupDirectiveFormSpec { |
| 36 | + public readonly value = model<string | string[]>(); |
| 37 | + public readonly type = input('single'); |
| 38 | +} |
| 39 | + |
| 40 | +describe('BrnToggleGroupDirective', () => { |
| 41 | + it('should allow only a single selected toggle button when type is single', async () => { |
| 42 | + const { getAllByRole } = await render(BrnToggleGroupDirectiveSpec); |
| 43 | + const buttons = getAllByRole('button'); |
| 44 | + |
| 45 | + expect(buttons[0]).toHaveAttribute('data-state', 'off'); |
| 46 | + expect(buttons[1]).toHaveAttribute('data-state', 'off'); |
| 47 | + expect(buttons[2]).toHaveAttribute('data-state', 'off'); |
| 48 | + |
| 49 | + await fireEvent.click(buttons[0]); |
| 50 | + expect(buttons[0]).toHaveAttribute('data-state', 'on'); |
| 51 | + expect(buttons[1]).toHaveAttribute('data-state', 'off'); |
| 52 | + expect(buttons[2]).toHaveAttribute('data-state', 'off'); |
| 53 | + |
| 54 | + await fireEvent.click(buttons[1]); |
| 55 | + expect(buttons[0]).toHaveAttribute('data-state', 'off'); |
| 56 | + expect(buttons[1]).toHaveAttribute('data-state', 'on'); |
| 57 | + expect(buttons[2]).toHaveAttribute('data-state', 'off'); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should allow multiple selected toggle buttons when type is multiple', async () => { |
| 61 | + const { getAllByRole, detectChanges } = await render(BrnToggleGroupDirectiveSpec, { |
| 62 | + inputs: { |
| 63 | + type: 'multiple', |
| 64 | + }, |
| 65 | + }); |
| 66 | + const buttons = getAllByRole('button'); |
| 67 | + |
| 68 | + expect(buttons[0]).toHaveAttribute('data-state', 'off'); |
| 69 | + expect(buttons[1]).toHaveAttribute('data-state', 'off'); |
| 70 | + expect(buttons[2]).toHaveAttribute('data-state', 'off'); |
| 71 | + |
| 72 | + await fireEvent.click(buttons[0]); |
| 73 | + detectChanges(); |
| 74 | + expect(buttons[0]).toHaveAttribute('data-state', 'on'); |
| 75 | + expect(buttons[1]).toHaveAttribute('data-state', 'off'); |
| 76 | + expect(buttons[2]).toHaveAttribute('data-state', 'off'); |
| 77 | + |
| 78 | + await fireEvent.click(buttons[1]); |
| 79 | + detectChanges(); |
| 80 | + expect(buttons[0]).toHaveAttribute('data-state', 'on'); |
| 81 | + expect(buttons[1]).toHaveAttribute('data-state', 'on'); |
| 82 | + expect(buttons[2]).toHaveAttribute('data-state', 'off'); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should disable all toggle buttons when disabled is true', async () => { |
| 86 | + const { getAllByRole } = await render(BrnToggleGroupDirectiveSpec, { |
| 87 | + inputs: { |
| 88 | + disabled: true, |
| 89 | + }, |
| 90 | + }); |
| 91 | + const buttons = getAllByRole('button'); |
| 92 | + |
| 93 | + expect(buttons[0]).toHaveAttribute('disabled'); |
| 94 | + expect(buttons[1]).toHaveAttribute('disabled'); |
| 95 | + expect(buttons[2]).toHaveAttribute('disabled'); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should initially select the button with the provided value (type = single)', async () => { |
| 99 | + const { getAllByRole, detectChanges } = await render(BrnToggleGroupDirectiveFormSpec, { |
| 100 | + inputs: { |
| 101 | + value: 'option-2', |
| 102 | + }, |
| 103 | + }); |
| 104 | + detectChanges(); |
| 105 | + const buttons = getAllByRole('button'); |
| 106 | + |
| 107 | + expect(buttons[0]).toHaveAttribute('data-state', 'off'); |
| 108 | + expect(buttons[1]).toHaveAttribute('data-state', 'on'); |
| 109 | + expect(buttons[2]).toHaveAttribute('data-state', 'off'); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should initially select the buttons with the provided values (type = multiple)', async () => { |
| 113 | + const { getAllByRole, detectChanges } = await render(BrnToggleGroupDirectiveFormSpec, { |
| 114 | + inputs: { |
| 115 | + value: ['option-1', 'option-3'], |
| 116 | + type: 'multiple', |
| 117 | + }, |
| 118 | + }); |
| 119 | + detectChanges(); |
| 120 | + const buttons = getAllByRole('button'); |
| 121 | + |
| 122 | + expect(buttons[0]).toHaveAttribute('data-state', 'on'); |
| 123 | + expect(buttons[1]).toHaveAttribute('data-state', 'off'); |
| 124 | + expect(buttons[2]).toHaveAttribute('data-state', 'on'); |
| 125 | + }); |
| 126 | + |
| 127 | + it('should initially select the button with the provided value (type = single) using ngModel', async () => { |
| 128 | + const { getAllByRole, detectChanges } = await render(BrnToggleGroupDirectiveFormSpec, { |
| 129 | + inputs: { |
| 130 | + value: 'option-2', |
| 131 | + }, |
| 132 | + }); |
| 133 | + detectChanges(); |
| 134 | + const buttons = getAllByRole('button'); |
| 135 | + |
| 136 | + expect(buttons[0]).toHaveAttribute('data-state', 'off'); |
| 137 | + expect(buttons[1]).toHaveAttribute('data-state', 'on'); |
| 138 | + expect(buttons[2]).toHaveAttribute('data-state', 'off'); |
| 139 | + }); |
| 140 | + |
| 141 | + it('should initially select the buttons with the provided values (type = multiple) using ngModel', async () => { |
| 142 | + const { getAllByRole, detectChanges } = await render(BrnToggleGroupDirectiveFormSpec, { |
| 143 | + inputs: { |
| 144 | + value: ['option-1', 'option-3'], |
| 145 | + type: 'multiple', |
| 146 | + }, |
| 147 | + }); |
| 148 | + detectChanges(); |
| 149 | + const buttons = getAllByRole('button'); |
| 150 | + |
| 151 | + expect(buttons[0]).toHaveAttribute('data-state', 'on'); |
| 152 | + expect(buttons[1]).toHaveAttribute('data-state', 'off'); |
| 153 | + expect(buttons[2]).toHaveAttribute('data-state', 'on'); |
| 154 | + }); |
| 155 | +}); |
0 commit comments