|
| 1 | +import { ComponentFixture, TestBed } from '@angular/core/testing'; |
| 2 | +import { ActionType, BroadcastIconsChangeMessage, IgxIconBroadcastService, SvgIcon, } from './icon.broadcast.service'; |
| 3 | +import { Component, SecurityContext } from '@angular/core'; |
| 4 | +import { IconMeta, IgxIconService } from 'igniteui-angular'; |
| 5 | +import { wait } from 'igniteui-angular/src/lib/test-utils/ui-interactions.spec'; |
| 6 | + |
| 7 | +describe('Icon broadcast service', () => { |
| 8 | + let fixture: ComponentFixture<BroadcastServiceComponent>; |
| 9 | + let broadcastChannel: BroadcastChannel; |
| 10 | + let events: BroadcastIconsChangeMessage[] = []; |
| 11 | + const buildIcon = |
| 12 | + '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path d="M22.7 19l-9.1-9.1c.9-2.3.4-5-1.5-6.9-2-2-5-2.4-7.4-1.3L9 6 6 9 1.6 4.7C.4 7.1.9 10.1 2.9 12.1c1.9 1.9 4.6 2.4 6.9 1.5l9.1 9.1c.4.4 1 .4 1.4 0l2.3-2.3c.5-.4.5-1.1.1-1.4z"/></svg>'; |
| 13 | + |
| 14 | + beforeEach(async () => { |
| 15 | + await TestBed.configureTestingModule({ |
| 16 | + imports: [], |
| 17 | + providers: [IgxIconBroadcastService] |
| 18 | + }) |
| 19 | + .compileComponents(); |
| 20 | + }); |
| 21 | + |
| 22 | + beforeEach(() => { |
| 23 | + broadcastChannel = new BroadcastChannel("ignite-ui-icon-channel"); |
| 24 | + broadcastChannel.onmessage = (e: MessageEvent<BroadcastIconsChangeMessage>) => { |
| 25 | + events.push(e.data); |
| 26 | + } |
| 27 | + fixture = TestBed.createComponent(BroadcastServiceComponent); |
| 28 | + }); |
| 29 | + |
| 30 | + afterEach(() => { |
| 31 | + events = []; |
| 32 | + broadcastChannel.close(); |
| 33 | + }); |
| 34 | + |
| 35 | + describe('Broadcast Events', () => { |
| 36 | + it('should correctly process event of icons registering on channel.', async() => { |
| 37 | + // simulate a new icon being registered on channel |
| 38 | + const icons: Map<string, Map<string, SvgIcon>> = new Map(); |
| 39 | + const icon: Map<string, SvgIcon> = new Map() |
| 40 | + icon.set("customIcon", { svg: buildIcon }); |
| 41 | + icons.set("customCollection", icon); |
| 42 | + const message: BroadcastIconsChangeMessage = { |
| 43 | + actionType: ActionType.RegisterIcon, |
| 44 | + collections: icons |
| 45 | + }; |
| 46 | + broadcastChannel.postMessage(message); |
| 47 | + fixture.detectChanges(); |
| 48 | + await wait(50); |
| 49 | + fixture.detectChanges(); |
| 50 | + const iconService = fixture.componentInstance.iconService; |
| 51 | + const svg = iconService.getSvgIcon("customIcon", "customCollection"); |
| 52 | + expect(svg).not.toBeUndefined(); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should correctly process event of setting an icon reference on channel.', async() => { |
| 56 | + const refs: Map<string, Map<string, IconMeta>> = new Map(); |
| 57 | + const ref: Map<string, IconMeta> = new Map() |
| 58 | + ref.set("customIcon", {name: "customIcon", family: "customCollection" }); |
| 59 | + refs.set("customCollection", ref); |
| 60 | + const message: BroadcastIconsChangeMessage = { |
| 61 | + actionType: ActionType.RegisterIcon, |
| 62 | + references: refs |
| 63 | + }; |
| 64 | + broadcastChannel.postMessage(message); |
| 65 | + fixture.detectChanges(); |
| 66 | + await wait(50); |
| 67 | + fixture.detectChanges(); |
| 68 | + |
| 69 | + const iconService = fixture.componentInstance.iconService; |
| 70 | + const serviceRef = iconService.getIconRef("customIcon", "customCollection"); |
| 71 | + expect(serviceRef.family).toBe("customCollection"); |
| 72 | + expect(serviceRef.name).toBe("customIcon"); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should send a request to sync state from any peer already on the channel on init.', async() => { |
| 76 | + await wait(50); |
| 77 | + expect(events.length).toBe(1); |
| 78 | + expect(events[0].actionType).toBe(ActionType.SyncState); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should correctly process event of synching full state of icons on channel.', async() => { |
| 82 | + const icons: Map<string, Map<string, SvgIcon>> = new Map(); |
| 83 | + const icon: Map<string, SvgIcon> = new Map() |
| 84 | + icon.set("customIcon", { svg: buildIcon }); |
| 85 | + icons.set("customCollection", icon); |
| 86 | + const refs: Map<string, Map<string, IconMeta>> = new Map(); |
| 87 | + const ref: Map<string, IconMeta> = new Map() |
| 88 | + ref.set("customIcon", {name: "customIcon", family: "customCollection" }); |
| 89 | + refs.set("customCollection", ref); |
| 90 | + const message: BroadcastIconsChangeMessage = { |
| 91 | + actionType: ActionType.SyncState, |
| 92 | + collections: icons, |
| 93 | + references: refs |
| 94 | + }; |
| 95 | + broadcastChannel.postMessage(message); |
| 96 | + await wait(50); |
| 97 | + const iconService = fixture.componentInstance.iconService; |
| 98 | + const svg = iconService.getSvgIcon("customIcon", "customCollection"); |
| 99 | + expect(svg).not.toBeUndefined(); |
| 100 | + const serviceRef = iconService.getIconRef("customIcon", "customCollection"); |
| 101 | + expect(serviceRef.family).toBe("customCollection"); |
| 102 | + expect(serviceRef.name).toBe("customIcon"); |
| 103 | + }); |
| 104 | + }) |
| 105 | +}); |
| 106 | + |
| 107 | +@Component({ |
| 108 | + template: ` |
| 109 | + `, |
| 110 | + standalone: true, |
| 111 | + providers: [IgxIconBroadcastService, IgxIconService] |
| 112 | +}) |
| 113 | +export class BroadcastServiceComponent { |
| 114 | + constructor(public iconBroadcast: IgxIconBroadcastService, public iconService: IgxIconService) {} |
| 115 | +} |
0 commit comments