import { EventTransceiverService } from '../services/event-transceiver.service'; import {Store} from '@ngrx/store'; import { EventTransceiverState } from '../reducers/event-transceiver.reducer'; import { PayloadVerificationService } from './payload-verification.service'; describe('Event Transceiver External Service', () => { let service: EventTransceiverService; let storeMock: Store; const payloadVerificationService = new PayloadVerificationService(); let logSpy; beforeAll(() => { logSpy = spyOn(console, 'log').and.returnValue(true); }); beforeEach(() => { storeMock = jasmine.createSpyObj>('Store', ['select', 'dispatch']); service = new EventTransceiverService(storeMock, payloadVerificationService); service.attachToWindow(); }); it('should have been created', () => { expect(service).toBeDefined(); }); it('should have its methods created', () => { expect(service.attachToWindow).toBeDefined(); expect(service.eventTrigger).toBeDefined(); }); it('should create EventTransceiver in window', () => { const fromWindow = window['EventTransceiver']; expect(fromWindow).toBeDefined(); }); it('should expose EventTrigger in window', () => { const fromWindow = window['EventTransceiver']['EventTrigger']; expect(fromWindow).toBeDefined(); }); it('should receive external event: [PIP] Click Element', () => { window['EventTransceiver']['EventTrigger']('[PIP] Click Element', {elementType: 'stickyNav', elementValue: 'overview'}); expect(storeMock.dispatch).toHaveBeenCalled(); }); it('should not process external events if the payload is not valid.', () => { const errorSpy = spyOn(service, 'handleIncorrectPayloadError').and.callThrough(); window['EventTransceiver']['EventTrigger']('[PIP] Click Element', {elementType: 'demo', elementValue: 'overview'}); expect(storeMock.dispatch).not.toHaveBeenCalled(); expect(errorSpy).toHaveBeenCalledWith('[PIP] Click Element'); }); it('should not process external events if the payload is undefined.', () => { const errorSpy = spyOn(service, 'handleIncorrectPayloadError').and.callThrough(); window['EventTransceiver']['EventTrigger']('[PIP] Click Element'); expect(storeMock.dispatch).not.toHaveBeenCalled(); expect(errorSpy).toHaveBeenCalledWith('[PIP] Click Element'); }); it('should receive external event: [PIP] Add To Cart', () => { window['EventTransceiver']['EventTrigger']('[PIP] Add To Cart', [ { product: { code: '101010' }, itemQuantity: 1, isCartSwitch: false, shippingMethod: 'bopis', addToCartLocation: 'primary', slotLocation: 1 } ]); expect(storeMock.dispatch).toHaveBeenCalled(); }); it('should not process external event [PIP] Add To Cart if the payload is not valid.', () => { const errorSpy = spyOn(service, 'handleIncorrectPayloadError').and.callThrough(); window['EventTransceiver']['EventTrigger']('[PIP] Add To Cart', { code: 10101, fulfillment: 'bopis'}); expect(storeMock.dispatch).not.toHaveBeenCalled(); expect(errorSpy).toHaveBeenCalledWith('[PIP] Add To Cart'); }); it('should not process external event [PIP] Add To Cart if the payload is undefined.', () => { const errorSpy = spyOn(service, 'handleIncorrectPayloadError').and.callThrough(); window['EventTransceiver']['EventTrigger']('[PIP] Add To Cart'); expect(storeMock.dispatch).not.toHaveBeenCalled(); expect(errorSpy).toHaveBeenCalledWith('[PIP] Add To Cart'); }); it('should receive external event: Super Sku Switch', () => { window['EventTransceiver']['EventTrigger']('Super Sku Switch', { productId: '1000807913', attributeId: 'vc-ls-item', attributeName: 'Color', targetProductId: '1000807916', targetValueId: 'vc-ls-item.Black', targetValueName: 'Black'}); expect(storeMock.dispatch).toHaveBeenCalled(); }); it('should not process external events if the payload is not valid.', () => { const errorSpy = spyOn(service, 'handleIncorrectPayloadError').and.callThrough(); window['EventTransceiver']['EventTrigger']('Super Sku Switch', {elementType: 'demo', elementValue: 'overview'}); expect(storeMock.dispatch).not.toHaveBeenCalled(); expect(errorSpy).toHaveBeenCalledWith('Super Sku Switch'); }); it('should not process external events if the payload is undefined.', () => { const errorSpy = spyOn(service, 'handleIncorrectPayloadError').and.callThrough(); window['EventTransceiver']['EventTrigger']('Super Sku Switch'); expect(storeMock.dispatch).not.toHaveBeenCalled(); expect(errorSpy).toHaveBeenCalledWith('Super Sku Switch'); }); it('#Carousel Item Click should not process external events if the payload is not valid.', () => { const errorSpy = spyOn(service, 'handleIncorrectPayloadError').and.callThrough(); window['EventTransceiver']['EventTrigger']('Carousel Item Click', {elementType: 'demo', elementValue: 'overview'}); expect(storeMock.dispatch).not.toHaveBeenCalled(); expect(errorSpy).toHaveBeenCalledWith('Carousel Item Click'); }); it('#Carousel Item Click should not process external events if the payload is undefined.', () => { const errorSpy = spyOn(service, 'handleIncorrectPayloadError').and.callThrough(); window['EventTransceiver']['EventTrigger']('Carousel Item Click'); expect(storeMock.dispatch).not.toHaveBeenCalled(); expect(errorSpy).toHaveBeenCalledWith('Carousel Item Click'); }); it('#Carousel Item Click should receive external event: [PIP] Super Sku Switch', () => { window['EventTransceiver']['EventTrigger']('Carousel Item Click', { productId: '1000807913', contextProductId: '1000807221', slotLocation: 4, productInteractionLocation: 'certona', productInteractionPageType: 'product page', productInteractionLocationTitle: 'Customers Who Viewed This Item Bought'}); expect(storeMock.dispatch).toHaveBeenCalled(); }); it('should not attempt to process unrecognized external events', () => { window['EventTransceiver']['EventTrigger']('Unknown event', { data : 'unknown'}); expect(storeMock.dispatch).not.toHaveBeenCalled(); expect(logSpy).toHaveBeenCalledWith('[Event Tranceiver External] Could not handle unrecognized external event: Unknown event'); }); it('should register a SetWhitelist callback on window when Whitelist is not available.', () => { window['EventTransceiver'] = {}; service.initializeWhitelist(); expect(window['EventTransceiver']['SetWhitelist']).toBeDefined(); }); it('should set whitelist in the Store when SetWhitelist is called with a valid payload.', () => { window['EventTransceiver'] = {}; service.initializeWhitelist(); const payload = { global: { analytics: ['event'] } }; window['EventTransceiver']['SetWhitelist'](payload); expect(storeMock.dispatch).toHaveBeenCalled(); }); it('should not set whitelist in the Store when SetWhitelist is called with an incorrect payload', () => { window['EventTransceiver'] = {}; service.initializeWhitelist(); const payload = {}; const errorSpy = spyOn(service, 'handleIncorrectPayloadError').and.callThrough(); window['EventTransceiver']['SetWhitelist'](payload); expect(storeMock.dispatch).not.toHaveBeenCalled(); expect(errorSpy).toHaveBeenCalledWith('Set Whitelist'); }); it('should set whitelist in the Store when Whitelist is available on window.', () => { window['EventTransceiverWhitelist'] = {global: {analytics: ['Event']}}; service.initializeWhitelist(); expect(storeMock.dispatch).toHaveBeenCalled(); }); });