|
| 1 | +import {HarnessLoader} from '@angular/cdk/testing'; |
| 2 | +import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed'; |
| 3 | +import {Component} from '@angular/core'; |
| 4 | +import {ComponentFixture, TestBed} from '@angular/core/testing'; |
| 5 | +import {MatBadgeModule, MatBadgePosition, MatBadgeSize} from '@angular/material/badge'; |
| 6 | +import {MatBadgeHarness} from '@angular/material/badge/testing/badge-harness'; |
| 7 | + |
| 8 | +/** Shared tests to run on both the original and MDC-based badges. */ |
| 9 | +export function runHarnessTests( |
| 10 | + badgeModule: typeof MatBadgeModule, badgeHarness: typeof MatBadgeHarness) { |
| 11 | + let fixture: ComponentFixture<BadgeHarnessTest>; |
| 12 | + let loader: HarnessLoader; |
| 13 | + |
| 14 | + beforeEach(async () => { |
| 15 | + await TestBed.configureTestingModule({ |
| 16 | + imports: [badgeModule], |
| 17 | + declarations: [BadgeHarnessTest], |
| 18 | + }).compileComponents(); |
| 19 | + |
| 20 | + fixture = TestBed.createComponent(BadgeHarnessTest); |
| 21 | + fixture.detectChanges(); |
| 22 | + loader = TestbedHarnessEnvironment.loader(fixture); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should load all badge harnesses', async () => { |
| 26 | + const badges = await loader.getAllHarnesses(badgeHarness); |
| 27 | + expect(badges.length).toBe(6); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should be able to get the text of a badge', async () => { |
| 31 | + const badge = await loader.getHarness(badgeHarness.with({selector: '#simple'})); |
| 32 | + |
| 33 | + expect(await badge.getText()).toBe('Simple badge'); |
| 34 | + fixture.componentInstance.simpleContent = 'Changed simple badge'; |
| 35 | + expect(await badge.getText()).toBe('Changed simple badge'); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should load badge with exact text', async () => { |
| 39 | + const badges = await loader.getAllHarnesses(badgeHarness.with({text: 'Simple badge'})); |
| 40 | + expect(badges.length).toBe(1); |
| 41 | + expect(await badges[0].getText()).toBe('Simple badge'); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should load badge with regex label match', async () => { |
| 45 | + const badges = await loader.getAllHarnesses(badgeHarness.with({text: /sized|disabled/i})); |
| 46 | + expect(badges.length).toBe(2); |
| 47 | + expect(await badges[0].getText()).toBe('Sized badge'); |
| 48 | + expect(await badges[1].getText()).toBe('Disabled badge'); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should get whether a badge is overlapping', async () => { |
| 52 | + const badge = await loader.getHarness(badgeHarness.with({selector: '#overlapping'})); |
| 53 | + |
| 54 | + expect(await badge.isOverlapping()).toBe(true); |
| 55 | + fixture.componentInstance.overlap = false; |
| 56 | + expect(await badge.isOverlapping()).toBe(false); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should get whether a badge is disabled', async () => { |
| 60 | + const badge = await loader.getHarness(badgeHarness.with({selector: '#disabled'})); |
| 61 | + |
| 62 | + expect(await badge.isDisabled()).toBe(true); |
| 63 | + fixture.componentInstance.disabled = false; |
| 64 | + expect(await badge.isDisabled()).toBe(false); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should get whether a badge is hidden', async () => { |
| 68 | + const badge = await loader.getHarness(badgeHarness.with({selector: '#hidden'})); |
| 69 | + |
| 70 | + expect(await badge.isHidden()).toBe(true); |
| 71 | + fixture.componentInstance.hidden = false; |
| 72 | + expect(await badge.isHidden()).toBe(false); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should get the position of a badge', async () => { |
| 76 | + const instance = fixture.componentInstance; |
| 77 | + const badge = await loader.getHarness(badgeHarness.with({selector: '#positioned'})); |
| 78 | + |
| 79 | + expect(await badge.getPosition()).toBe('above after'); |
| 80 | + |
| 81 | + instance.position = 'below'; |
| 82 | + expect(await badge.getPosition()).toBe('below after'); |
| 83 | + |
| 84 | + instance.position = 'below before'; |
| 85 | + expect(await badge.getPosition()).toBe('below before'); |
| 86 | + |
| 87 | + instance.position = 'above'; |
| 88 | + expect(await badge.getPosition()).toBe('above after'); |
| 89 | + |
| 90 | + instance.position = 'above before'; |
| 91 | + expect(await badge.getPosition()).toBe('above before'); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should get the size of a badge', async () => { |
| 95 | + const instance = fixture.componentInstance; |
| 96 | + const badge = await loader.getHarness(badgeHarness.with({selector: '#sized'})); |
| 97 | + |
| 98 | + expect(await badge.getSize()).toBe('medium'); |
| 99 | + |
| 100 | + instance.size = 'small'; |
| 101 | + expect(await badge.getSize()).toBe('small'); |
| 102 | + |
| 103 | + instance.size = 'large'; |
| 104 | + expect(await badge.getSize()).toBe('large'); |
| 105 | + }); |
| 106 | +} |
| 107 | + |
| 108 | + |
| 109 | +@Component({ |
| 110 | + template: ` |
| 111 | + <button id="simple" [matBadge]="simpleContent">Simple</button> |
| 112 | + <button |
| 113 | + id="positioned" |
| 114 | + matBadge="Positioned badge" |
| 115 | + [matBadgePosition]="position">Positioned</button> |
| 116 | + <button |
| 117 | + id="sized" |
| 118 | + matBadge="Sized badge" |
| 119 | + [matBadgeSize]="size">Sized</button> |
| 120 | + <button |
| 121 | + id="overlapping" |
| 122 | + matBadge="Overlapping badge" |
| 123 | + [matBadgeOverlap]="overlap">Overlapping</button> |
| 124 | + <button |
| 125 | + id="hidden" |
| 126 | + matBadge="Hidden badge" |
| 127 | + [matBadgeHidden]="hidden">Hidden</button> |
| 128 | + <button |
| 129 | + id="disabled" |
| 130 | + matBadge="Disabled badge" |
| 131 | + [matBadgeDisabled]="disabled">Disabled</button> |
| 132 | + ` |
| 133 | +}) |
| 134 | +class BadgeHarnessTest { |
| 135 | + simpleContent = 'Simple badge'; |
| 136 | + position: MatBadgePosition = 'above after'; |
| 137 | + size: MatBadgeSize = 'medium'; |
| 138 | + overlap = true; |
| 139 | + hidden = true; |
| 140 | + disabled = true; |
| 141 | +} |
| 142 | + |
0 commit comments