|
| 1 | +import {Component, signal} from '@angular/core'; |
| 2 | +import {ComponentFixture, TestBed} from '@angular/core/testing'; |
| 3 | +import {HarnessLoader, parallel} from '@angular/cdk/testing'; |
| 4 | +import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed'; |
| 5 | +import {Toolbar} from '../toolbar'; |
| 6 | +import {ToolbarWidget} from '../toolbar-widget'; |
| 7 | +import {ToolbarWidgetGroup} from '../toolbar-widget-group'; |
| 8 | +import {ToolbarHarness} from './toolbar-harness'; |
| 9 | +import {ToolbarWidgetGroupHarness} from './toolbar-widget-group-harness'; |
| 10 | +import {ToolbarWidgetHarness} from './toolbar-widget-harness'; |
| 11 | + |
| 12 | +describe('ToolbarHarness', () => { |
| 13 | + let fixture: ComponentFixture<ToolbarHarnessTest>; |
| 14 | + let loader: HarnessLoader; |
| 15 | + |
| 16 | + beforeEach(() => { |
| 17 | + fixture = TestBed.createComponent(ToolbarHarnessTest); |
| 18 | + fixture.detectChanges(); |
| 19 | + loader = TestbedHarnessEnvironment.documentRootLoader(fixture); |
| 20 | + }); |
| 21 | + |
| 22 | + it('should be able to load toolbar harnesses', async () => { |
| 23 | + const harnesses = await loader.getAllHarnesses(ToolbarHarness); |
| 24 | + expect(harnesses.length).toBe(1); |
| 25 | + }); |
| 26 | + |
| 27 | + it('should be able to load toolbar widget group harnesses', async () => { |
| 28 | + const harnesses = await loader.getAllHarnesses(ToolbarWidgetGroupHarness); |
| 29 | + expect(harnesses.length).toBe(2); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should be able to load toolbar widget harnesses', async () => { |
| 33 | + const harnesses = await loader.getAllHarnesses(ToolbarWidgetHarness); |
| 34 | + expect(harnesses.length).toBe(8); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should be able to get the widgets in a toolbar', async () => { |
| 38 | + const toolbar = await loader.getHarness(ToolbarHarness); |
| 39 | + const widgets = await toolbar.getWidgets(); |
| 40 | + expect(widgets.length).toBe(8); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should be able to get the widget groups in a toolbar', async () => { |
| 44 | + const toolbar = await loader.getHarness(ToolbarHarness); |
| 45 | + const groups = await toolbar.getWidgetGroups(); |
| 46 | + expect(groups.length).toBe(2); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should be able to get the toolbar orientation', async () => { |
| 50 | + const toolbar = await loader.getHarness(ToolbarHarness); |
| 51 | + expect(await toolbar.getOrientation()).toBe('horizontal'); |
| 52 | + |
| 53 | + fixture.componentInstance.orientation.set('vertical'); |
| 54 | + expect(await toolbar.getOrientation()).toBe('vertical'); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should be able to get whether the toolbar is disabled', async () => { |
| 58 | + const toolbar = await loader.getHarness(ToolbarHarness); |
| 59 | + expect(await toolbar.isDisabled()).toBe(false); |
| 60 | + |
| 61 | + fixture.componentInstance.toolbarDisabled.set(true); |
| 62 | + expect(await toolbar.isDisabled()).toBe(true); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should be able to get the widgets in a widget group', async () => { |
| 66 | + const group = await loader.getHarness(ToolbarWidgetGroupHarness); |
| 67 | + const widgets = await group.getWidgets(); |
| 68 | + expect(widgets.length).toBe(3); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should be able to load a widget harness by text', async () => { |
| 72 | + const harnesses = await loader.getAllHarnesses(ToolbarWidgetHarness.with({text: /^Align/})); |
| 73 | + expect(harnesses.length).toBe(3); |
| 74 | + expect(await parallel(() => harnesses.map(harness => harness.getText()))).toEqual([ |
| 75 | + 'Align left', |
| 76 | + 'Align center', |
| 77 | + 'Align right', |
| 78 | + ]); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should be able to toggle the active state of a widget', async () => { |
| 82 | + const widget = await loader.getHarness(ToolbarWidgetHarness.with({text: 'Align left'})); |
| 83 | + expect(await widget.isActive()).toBe(false); |
| 84 | + |
| 85 | + await widget.click(); |
| 86 | + expect(await widget.isActive()).toBe(true); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should be able to get whether a widget is disabled', async () => { |
| 90 | + const widget = await loader.getHarness(ToolbarWidgetHarness.with({text: 'Undo'})); |
| 91 | + expect(await widget.isDisabled()).toBe(false); |
| 92 | + |
| 93 | + fixture.componentInstance.undoDisabled.set(true); |
| 94 | + expect(await widget.isDisabled()).toBe(true); |
| 95 | + }); |
| 96 | +}); |
| 97 | + |
| 98 | +@Component({ |
| 99 | + template: ` |
| 100 | + <div ngToolbar [orientation]="orientation()" [disabled]="toolbarDisabled()"> |
| 101 | + <button ngToolbarWidget value="undo" [disabled]="undoDisabled()">Undo</button> |
| 102 | + <button ngToolbarWidget value="redo">Redo</button> |
| 103 | +
|
| 104 | + <div ngToolbarWidgetGroup> |
| 105 | + <button ngToolbarWidget value="bold">Bold</button> |
| 106 | + <button ngToolbarWidget value="italic">Italic</button> |
| 107 | + <button ngToolbarWidget value="underlined">Underlined</button> |
| 108 | + </div> |
| 109 | +
|
| 110 | + <div ngToolbarWidgetGroup> |
| 111 | + <button ngToolbarWidget value="aling-left">Align left</button> |
| 112 | + <button ngToolbarWidget value="aling-center">Align center</button> |
| 113 | + <button ngToolbarWidget value="aling-right">Align right</button> |
| 114 | + </div> |
| 115 | + </div> |
| 116 | + `, |
| 117 | + imports: [Toolbar, ToolbarWidget, ToolbarWidgetGroup], |
| 118 | +}) |
| 119 | +class ToolbarHarnessTest { |
| 120 | + orientation = signal<'vertical' | 'horizontal'>('horizontal'); |
| 121 | + toolbarDisabled = signal(false); |
| 122 | + undoDisabled = signal(false); |
| 123 | +} |
0 commit comments