|
| 1 | +// Copyright 2026 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +import { getHarness } from '@angular/cdk/testing/catalyst/async'; |
| 15 | +import { TestBed } from '@angular/core/testing'; |
| 16 | +import { MatChipHarness } from '@angular/material/chips/testing'; |
| 17 | +import { NoopAnimationsModule } from '@angular/platform-browser/animations'; |
| 18 | + |
| 19 | +import { bootstrap, setupModule } from 'app/testing/test'; |
| 20 | + |
| 21 | +import { StatusChip, StatusOption } from './status_chip'; |
| 22 | + |
| 23 | +describe('StatusChip Component', () => { |
| 24 | + beforeEach(() => { |
| 25 | + setupModule({ |
| 26 | + imports: [StatusChip, NoopAnimationsModule], |
| 27 | + }); |
| 28 | + }); |
| 29 | + |
| 30 | + it('displays status with optional replica count', async () => { |
| 31 | + bootstrap(StatusChip, { status: StatusOption.RUNNING, replicas: 4 }); |
| 32 | + |
| 33 | + const chipHarness = await getHarness(MatChipHarness); |
| 34 | + expect(await chipHarness.getText()).toBe('4 Running'); |
| 35 | + }); |
| 36 | + |
| 37 | + const expectedColors: ReadonlyArray<{ status: StatusOption; expectedClass: string }> = [ |
| 38 | + { status: StatusOption.SUCCESS, expectedClass: 'green' }, |
| 39 | + { status: StatusOption.COMPLETED, expectedClass: 'green' }, |
| 40 | + { status: StatusOption.STARTING, expectedClass: 'light-green' }, |
| 41 | + { status: StatusOption.FAILED, expectedClass: 'red' }, |
| 42 | + { status: StatusOption.RUNNING, expectedClass: 'blue' }, |
| 43 | + { status: StatusOption.NOT_RUNNING, expectedClass: 'blue' }, |
| 44 | + { status: StatusOption.PENDING, expectedClass: 'yellow' }, |
| 45 | + { status: StatusOption.INITIALIZING, expectedClass: 'yellow' }, |
| 46 | + { status: StatusOption.UNKNOWN, expectedClass: 'yellow' }, |
| 47 | + { status: StatusOption.RESTARTING, expectedClass: 'yellow' }, |
| 48 | + { status: StatusOption.DESCHEDULING, expectedClass: 'grey' }, |
| 49 | + { status: StatusOption.SCHEDULING, expectedClass: 'grey' }, |
| 50 | + { status: StatusOption.SCHEDULING_PAUSED, expectedClass: 'orange' }, |
| 51 | + { status: StatusOption.STOPPING, expectedClass: 'black' }, |
| 52 | + { status: StatusOption.STOPPED, expectedClass: 'black' }, |
| 53 | + ]; |
| 54 | + |
| 55 | + for (const tc of expectedColors) { |
| 56 | + it(`applies ${tc.expectedClass} class and keeps mat-mdc-chip for ${tc.status}`, async () => { |
| 57 | + bootstrap(StatusChip, { status: tc.status }); |
| 58 | + |
| 59 | + const chipHarness = await getHarness(MatChipHarness); |
| 60 | + const host = await chipHarness.host(); |
| 61 | + |
| 62 | + expect(await host.hasClass('mat-mdc-chip')) |
| 63 | + .withContext('Expected host Material chip class to be preserved') |
| 64 | + .toBeTrue(); |
| 65 | + expect(await host.hasClass(tc.expectedClass)) |
| 66 | + .withContext(`Expected status "${tc.status}" to apply class "${tc.expectedClass}"`) |
| 67 | + .toBeTrue(); |
| 68 | + }); |
| 69 | + } |
| 70 | + |
| 71 | + it('updates chip color class dynamically when status changes without dropping host classes', () => { |
| 72 | + const fixture = TestBed.createComponent(StatusChip); |
| 73 | + fixture.componentRef.setInput('status', StatusOption.RUNNING); |
| 74 | + fixture.detectChanges(); |
| 75 | + |
| 76 | + const chipEl = fixture.nativeElement.querySelector('mat-chip') as HTMLElement; |
| 77 | + expect(chipEl.classList.contains('mat-mdc-chip')).toBeTrue(); |
| 78 | + expect(chipEl.classList.contains('blue')).toBeTrue(); |
| 79 | + |
| 80 | + fixture.componentRef.setInput('status', StatusOption.COMPLETED); |
| 81 | + fixture.detectChanges(); |
| 82 | + |
| 83 | + expect(chipEl.classList.contains('mat-mdc-chip')).toBeTrue(); |
| 84 | + expect(chipEl.classList.contains('blue')).toBeFalse(); |
| 85 | + expect(chipEl.classList.contains('green')).toBeTrue(); |
| 86 | + }); |
| 87 | +}); |
0 commit comments