|
| 1 | +/** |
| 2 | + * Copyright 2026 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import {ComponentFixture, TestBed} from '@angular/core/testing'; |
| 18 | +import {signal as angularSignal} from '@angular/core'; |
| 19 | +import {CheckBoxComponent} from './check-box.component'; |
| 20 | +import {A2uiRendererService} from '../../core/a2ui-renderer.service'; |
| 21 | +import {ComponentBinder} from '../../core/component-binder.service'; |
| 22 | +import {setComponentProps, createBoundProperty, ComponentToProps} from '../../core/test-utils'; |
| 23 | + |
| 24 | +describe('CheckBoxComponent', () => { |
| 25 | + let component: CheckBoxComponent; |
| 26 | + let fixture: ComponentFixture<CheckBoxComponent>; |
| 27 | + let mockRendererService: {surfaceGroup: {getSurface: jasmine.Spy}}; |
| 28 | + let defaultProps: ComponentToProps<CheckBoxComponent>; |
| 29 | + |
| 30 | + beforeEach(async () => { |
| 31 | + mockRendererService = { |
| 32 | + surfaceGroup: { |
| 33 | + getSurface: jasmine.createSpy('getSurface').and.returnValue({ |
| 34 | + componentsModel: new Map(), |
| 35 | + catalog: { |
| 36 | + id: 'mock-catalog', |
| 37 | + components: new Map(), |
| 38 | + }, |
| 39 | + }), |
| 40 | + }, |
| 41 | + }; |
| 42 | + const mockBinder = jasmine.createSpyObj('ComponentBinder', ['bind']); |
| 43 | + |
| 44 | + await TestBed.configureTestingModule({ |
| 45 | + imports: [CheckBoxComponent], |
| 46 | + providers: [ |
| 47 | + {provide: A2uiRendererService, useValue: mockRendererService}, |
| 48 | + {provide: ComponentBinder, useValue: mockBinder}, |
| 49 | + ], |
| 50 | + }).compileComponents(); |
| 51 | + |
| 52 | + fixture = TestBed.createComponent(CheckBoxComponent); |
| 53 | + component = fixture.componentInstance; |
| 54 | + fixture.componentRef.setInput('surfaceId', 'test-surface'); |
| 55 | + fixture.componentRef.setInput('dataContextPath', '/'); |
| 56 | + |
| 57 | + defaultProps = { |
| 58 | + label: createBoundProperty(''), |
| 59 | + value: createBoundProperty(false), |
| 60 | + isValid: createBoundProperty(true), |
| 61 | + validationErrors: createBoundProperty<string[]>([]), |
| 62 | + }; |
| 63 | + setComponentProps(fixture, defaultProps); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should create', () => { |
| 67 | + fixture.detectChanges(); |
| 68 | + expect(component).toBeTruthy(); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should show label and checked state', () => { |
| 72 | + setComponentProps(fixture, { |
| 73 | + ...defaultProps, |
| 74 | + label: createBoundProperty('Check me'), |
| 75 | + value: createBoundProperty(true), |
| 76 | + }); |
| 77 | + fixture.detectChanges(); |
| 78 | + const input = fixture.nativeElement.querySelector('input'); |
| 79 | + expect(input.checked).toBe(true); |
| 80 | + expect(fixture.nativeElement.textContent).toContain('Check me'); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should call onUpdate when toggled', () => { |
| 84 | + const onUpdateSpy = jasmine.createSpy('onUpdate'); |
| 85 | + setComponentProps(fixture, { |
| 86 | + ...defaultProps, |
| 87 | + value: {value: angularSignal(false), raw: false, onUpdate: onUpdateSpy}, |
| 88 | + }); |
| 89 | + fixture.detectChanges(); |
| 90 | + const input = fixture.nativeElement.querySelector('input'); |
| 91 | + input.click(); |
| 92 | + expect(onUpdateSpy).toHaveBeenCalledWith(true); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should apply primary color when checked', () => { |
| 96 | + setComponentProps(fixture, { |
| 97 | + ...defaultProps, |
| 98 | + value: createBoundProperty(true), |
| 99 | + }); |
| 100 | + mockRendererService.surfaceGroup.getSurface.and.returnValue({ |
| 101 | + theme: {primaryColor: 'rgb(255, 0, 0)'}, |
| 102 | + componentsModel: new Map(), |
| 103 | + catalog: {components: new Map()}, |
| 104 | + }); |
| 105 | + fixture.detectChanges(); |
| 106 | + |
| 107 | + const input = fixture.nativeElement.querySelector('input'); |
| 108 | + const styles = window.getComputedStyle(input); |
| 109 | + |
| 110 | + expect(styles.accentColor).toBe('rgb(255, 0, 0)'); |
| 111 | + }); |
| 112 | +}); |
0 commit comments