|
1 | | -import { render } from '@testing-library/angular'; |
| 1 | +import { HarnessLoader } from '@angular/cdk/testing'; |
| 2 | +import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed'; |
| 3 | +import { MatButtonHarness } from '@angular/material/button/testing'; |
| 4 | +import { MatCheckboxHarness } from '@angular/material/checkbox/testing'; |
| 5 | +import { MatInputHarness } from '@angular/material/input/testing'; |
| 6 | +import { |
| 7 | + MatSliderHarness, |
| 8 | + MatSliderThumbHarness, |
| 9 | +} from '@angular/material/slider/testing'; |
| 10 | +import { render, RenderResult, screen } from '@testing-library/angular'; |
2 | 11 | import { ChildComponent } from './child.component'; |
3 | 12 |
|
4 | | -describe('ChildComponent', () => { |
| 13 | +describe.only('ChildComponent', () => { |
| 14 | + let view: RenderResult<ChildComponent, ChildComponent>; |
| 15 | + let loader: HarnessLoader; |
| 16 | + |
| 17 | + beforeEach(async () => { |
| 18 | + view = await render(ChildComponent); |
| 19 | + loader = TestbedHarnessEnvironment.documentRootLoader(view.fixture); |
| 20 | + }); |
| 21 | + |
5 | 22 | describe('When init', () => { |
6 | 23 | test('Then show 1 slider, 3 checkboxes, 4 inputs, 2 buttons', async () => { |
7 | | - await render(ChildComponent); |
| 24 | + expect(screen.getByTestId('mat-slider')).toBeInTheDocument(); |
| 25 | + expect(screen.getAllByRole('checkbox').length).toBe(3); |
| 26 | + expect(screen.getAllByRole('button').length).toBe(2); |
8 | 27 | }); |
9 | 28 |
|
10 | 29 | test('Then initial value of slider thumb is 0', async () => { |
11 | | - await render(ChildComponent); |
| 30 | + const sliderThumbHarness = await loader.getHarness(MatSliderThumbHarness); |
| 31 | + |
| 32 | + expect(await sliderThumbHarness.getValue()).toBe(0); |
| 33 | + expect(await sliderThumbHarness.getDisplayValue()).toBe('0'); |
12 | 34 | }); |
13 | 35 | }); |
14 | 36 |
|
15 | 37 | describe('Given maxValue set to 109', () => { |
16 | 38 | test('Then slider max value is 109', async () => { |
17 | | - await render(ChildComponent); |
| 39 | + const sliderHarness = await loader.getHarness(MatSliderHarness); |
| 40 | + const maxValueInput = loader.getHarness( |
| 41 | + MatInputHarness.with({ selector: '#input-max' }), |
| 42 | + ); |
| 43 | + |
| 44 | + (await maxValueInput).setValue('109'); |
| 45 | + |
| 46 | + expect(await sliderHarness.getMaxValue()).toBe(109); |
18 | 47 | }); |
19 | 48 | }); |
20 | 49 |
|
21 | 50 | describe('When disabled checkbox is toggled', () => { |
22 | 51 | test('Then slider is disabled', async () => { |
23 | | - await render(ChildComponent); |
| 52 | + const sliderHarness = await loader.getHarness(MatSliderHarness); |
| 53 | + const disabledCheckbox = await loader.getHarness( |
| 54 | + MatCheckboxHarness.with({ |
| 55 | + selector: '[data-testid="disable-checkbox"]', |
| 56 | + }), |
| 57 | + ); |
| 58 | + |
| 59 | + await disabledCheckbox.check(); |
| 60 | + |
| 61 | + expect(await sliderHarness.isDisabled()).toBe(true); |
24 | 62 | }); |
25 | 63 | }); |
26 | 64 |
|
27 | 65 | describe('Given step value set to 5, and When clicking on forward button two times', () => { |
28 | 66 | test('Then thumb value is 10', async () => { |
29 | | - await render(ChildComponent); |
| 67 | + const sliderThumbHarness = await loader.getHarness(MatSliderThumbHarness); |
| 68 | + const stepInput = await loader.getHarness( |
| 69 | + MatInputHarness.with({ selector: '#input-step' }), |
| 70 | + ); |
| 71 | + const forwardButton = await loader.getHarness( |
| 72 | + MatButtonHarness.with({ |
| 73 | + selector: 'button[data-testid="arrow-forward-btn"]', |
| 74 | + }), |
| 75 | + ); |
| 76 | + |
| 77 | + await stepInput.setValue('5'); |
| 78 | + await forwardButton.click(); |
| 79 | + await forwardButton.click(); |
| 80 | + |
| 81 | + expect(await sliderThumbHarness.getValue()).toBe(10); |
30 | 82 | }); |
31 | 83 | }); |
32 | 84 |
|
33 | 85 | describe('Given slider value set to 5, and step value to 6 and When clicking on back button', () => { |
34 | 86 | test('Then slider value is still 5', async () => { |
35 | | - await render(ChildComponent); |
| 87 | + const sliderThumbHarness = await loader.getHarness(MatSliderThumbHarness); |
| 88 | + const stepInput = await loader.getHarness( |
| 89 | + MatInputHarness.with({ selector: '#input-step' }), |
| 90 | + ); |
| 91 | + const backButton = await loader.getHarness( |
| 92 | + MatButtonHarness.with({ |
| 93 | + selector: 'button[data-testid="arrow-back-btn"]', |
| 94 | + }), |
| 95 | + ); |
| 96 | + |
| 97 | + await sliderThumbHarness.setValue(5); |
| 98 | + await stepInput.setValue('6'); |
| 99 | + await backButton.click(); |
| 100 | + |
| 101 | + expect(await sliderThumbHarness.getValue()).toBe(5); |
36 | 102 | }); |
37 | 103 | }); |
38 | 104 | }); |
0 commit comments