|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2025 Google LLC |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | + |
| 19 | +import {MessageProcessor} from '@a2ui/angular'; |
| 20 | +import {Types} from '@a2ui/lit/0.8'; |
| 21 | +import {SimpleChanges} from '@angular/core'; |
| 22 | +import {ComponentFixture, TestBed} from '@angular/core/testing'; |
| 23 | +// 1p-ONLY-IMPORTS: import {beforeEach, describe, expect, it} |
| 24 | + |
| 25 | +import {initTestBed} from '../../testing/utils'; |
| 26 | + |
| 27 | +import {A2uiCanvasComponent} from './a2ui-canvas.component'; |
| 28 | + |
| 29 | +describe('A2uiCanvasComponent', () => { |
| 30 | + let component: A2uiCanvasComponent; |
| 31 | + let fixture: ComponentFixture<A2uiCanvasComponent>; |
| 32 | + let mockMessageProcessor: jasmine.SpyObj<MessageProcessor>; |
| 33 | + |
| 34 | + beforeEach(async () => { |
| 35 | + initTestBed(); |
| 36 | + mockMessageProcessor = jasmine.createSpyObj<MessageProcessor>( |
| 37 | + 'MessageProcessor', ['processMessages', 'getSurfaces']); |
| 38 | + mockMessageProcessor.getSurfaces.and.returnValue(new Map()); |
| 39 | + |
| 40 | + await TestBed |
| 41 | + .configureTestingModule({ |
| 42 | + imports: [A2uiCanvasComponent], |
| 43 | + providers: [ |
| 44 | + {provide: MessageProcessor, useValue: mockMessageProcessor}, |
| 45 | + ], |
| 46 | + }) |
| 47 | + .compileComponents(); |
| 48 | + |
| 49 | + fixture = TestBed.createComponent(A2uiCanvasComponent); |
| 50 | + component = fixture.componentInstance; |
| 51 | + fixture.detectChanges(); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should create', () => { |
| 55 | + expect(component).toBeTruthy(); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should process beginRendering message', () => { |
| 59 | + const message = { |
| 60 | + beginRendering: { |
| 61 | + surfaceId: 'sales_data_yearly_surface', |
| 62 | + root: 'root-column', |
| 63 | + styles: {primaryColor: '#00BFFF', font: 'Arial'} |
| 64 | + } |
| 65 | + } as unknown as Types.ServerToClientMessage; |
| 66 | + component.beginRendering = message; |
| 67 | + |
| 68 | + const changes: SimpleChanges = { |
| 69 | + beginRendering: { |
| 70 | + currentValue: message, |
| 71 | + previousValue: null, |
| 72 | + firstChange: true, |
| 73 | + isFirstChange: () => true |
| 74 | + } |
| 75 | + }; |
| 76 | + component.ngOnChanges(changes); |
| 77 | + |
| 78 | + expect(mockMessageProcessor.processMessages).toHaveBeenCalledWith([message]); |
| 79 | + expect(component.surfaceId()).toBe('sales_data_yearly_surface'); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should process surfaceUpdate message', () => { |
| 83 | + const message = { |
| 84 | + surfaceUpdate: { |
| 85 | + surfaceId: 'sales_data_yearly_surface', |
| 86 | + components: [ |
| 87 | + { |
| 88 | + id: 'root-column', |
| 89 | + component: { |
| 90 | + Column: { |
| 91 | + children: {explicitList: ['chart-title', 'category-list']} |
| 92 | + } |
| 93 | + } |
| 94 | + }, |
| 95 | + { |
| 96 | + id: 'chart-title', |
| 97 | + component: { |
| 98 | + Text: {text: {path: 'chart.title'}, usageHint: 'h2'} |
| 99 | + } |
| 100 | + }, |
| 101 | + { |
| 102 | + id: 'category-list', |
| 103 | + component: { |
| 104 | + List: { |
| 105 | + direction: 'vertical', |
| 106 | + children: { |
| 107 | + template: { |
| 108 | + componentId: 'category-item-template', |
| 109 | + dataBinding: '/chart.items' |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + }, |
| 115 | + { |
| 116 | + id: 'category-item-template', |
| 117 | + component: {Card: {child: 'item-row'}} |
| 118 | + }, |
| 119 | + { |
| 120 | + id: 'item-row', |
| 121 | + component: { |
| 122 | + Row: { |
| 123 | + distribution: 'spaceBetween', |
| 124 | + children: {explicitList: ['item-label', 'item-value']} |
| 125 | + } |
| 126 | + } |
| 127 | + }, |
| 128 | + { |
| 129 | + id: 'item-label', |
| 130 | + component: {Text: {text: {path: 'label'}}} |
| 131 | + }, |
| 132 | + { |
| 133 | + id: 'item-value', |
| 134 | + component: {Text: {text: {path: 'value'}}} |
| 135 | + } |
| 136 | + ] |
| 137 | + } |
| 138 | + } as unknown as Types.ServerToClientMessage; |
| 139 | + component.surfaceUpdate = message; |
| 140 | + |
| 141 | + const changes: SimpleChanges = { |
| 142 | + surfaceUpdate: { |
| 143 | + currentValue: message, |
| 144 | + previousValue: null, |
| 145 | + firstChange: true, |
| 146 | + isFirstChange: () => true |
| 147 | + } |
| 148 | + }; |
| 149 | + component.ngOnChanges(changes); |
| 150 | + |
| 151 | + expect(mockMessageProcessor.processMessages).toHaveBeenCalledWith([message]); |
| 152 | + expect(component.surfaceId()).toBe('sales_data_yearly_surface'); |
| 153 | + }); |
| 154 | + |
| 155 | + it('should process dataModelUpdate message', () => { |
| 156 | + const message = { |
| 157 | + dataModelUpdate: { |
| 158 | + surfaceId: 'sales_data_yearly_surface', |
| 159 | + path: '/', |
| 160 | + contents: [ |
| 161 | + {key: 'chart.title', valueString: 'Yearly Sales by Category'}, |
| 162 | + {key: 'chart.items[0].label', valueString: 'Apparel'}, |
| 163 | + {key: 'chart.items[0].value', valueNumber: 41}, |
| 164 | + {key: 'chart.items[1].label', valueString: 'Home Goods'}, |
| 165 | + {key: 'chart.items[1].value', valueNumber: 15}, |
| 166 | + {key: 'chart.items[2].label', valueString: 'Electronics'}, |
| 167 | + {key: 'chart.items[2].value', valueNumber: 28}, |
| 168 | + {key: 'chart.items[3].label', valueString: 'Health & Beauty'}, |
| 169 | + {key: 'chart.items[3].value', valueNumber: 10}, |
| 170 | + {key: 'chart.items[4].label', valueString: 'Other'}, |
| 171 | + {key: 'chart.items[4].value', valueNumber: 6} |
| 172 | + ] |
| 173 | + } |
| 174 | + } as unknown as Types.ServerToClientMessage; |
| 175 | + component.dataModelUpdate = message; |
| 176 | + |
| 177 | + const changes: SimpleChanges = { |
| 178 | + dataModelUpdate: { |
| 179 | + currentValue: message, |
| 180 | + previousValue: null, |
| 181 | + firstChange: true, |
| 182 | + isFirstChange: () => true |
| 183 | + } |
| 184 | + }; |
| 185 | + component.ngOnChanges(changes); |
| 186 | + |
| 187 | + expect(mockMessageProcessor.processMessages).toHaveBeenCalledWith([message]); |
| 188 | + expect(component.surfaceId()).toBe('sales_data_yearly_surface'); |
| 189 | + }); |
| 190 | + |
| 191 | + it('should update activeSurface when surfaceId matches', () => { |
| 192 | + const surfaceId = 'sales_data_yearly_surface'; |
| 193 | + const mockSurface = {} as Types.Surface; |
| 194 | + const surfaces = new Map<string, Types.Surface>([[surfaceId, mockSurface]]); |
| 195 | + mockMessageProcessor.getSurfaces.and.returnValue(surfaces); |
| 196 | + |
| 197 | + const message = { |
| 198 | + beginRendering: {surfaceId: surfaceId, root: 'root-column'} |
| 199 | + } as unknown as Types.ServerToClientMessage; |
| 200 | + |
| 201 | + component.beginRendering = message; |
| 202 | + component.ngOnChanges({ |
| 203 | + beginRendering: { |
| 204 | + currentValue: message, |
| 205 | + previousValue: null, |
| 206 | + firstChange: true, |
| 207 | + isFirstChange: () => true |
| 208 | + } |
| 209 | + }); |
| 210 | + |
| 211 | + expect(component.activeSurface()).toBe(mockSurface); |
| 212 | + }); |
| 213 | +}); |
0 commit comments