import { ClassroomEventHandler } from '../eventHandlers'; import { ScreenshotCaptureService } from '../screenshotCaptureService'; import { WebViewDispatcher } from '../webViewDispatcher'; let whMock = jest.fn(); jest.mock('../extensionComm', () => { return { extensionComm: { //@ts-ignore sendToWebHelper: (data) => { whMock(data); } } }; }); afterEach(() => { jest.clearAllMocks(); jest.resetAllMocks(); jest.resetModules(); }); describe('Classroom Event Handlers', () => { it('onJoinClassPostProcess sets the state and calls subscriber', () => { const classData = { className: 'Advanced Maths' }; const mockSubscriber = { onJoinClass: jest.fn() }; const ceh = new ClassroomEventHandler({} as WebViewDispatcher, {} as ScreenshotCaptureService); //@ts-ignore const setStateStub = jest.spyOn(ClassroomEventHandler.prototype, 'setState').mockImplementation(() => {}); //@ts-ignore ceh.addSubscriber(mockSubscriber); ceh.onJoinClassPostProcess(classData as any); expect(setStateStub).toHaveBeenCalled(); expect(mockSubscriber.onJoinClass).toHaveBeenCalledWith(classData); }); it('onRemoteExecute sends the launch command to the extension when there is a valid url', async () => { const ceh = new ClassroomEventHandler({} as WebViewDispatcher, {} as ScreenshotCaptureService); ceh.onRemoteExecute({ path: 'http://www.google.com', teacherInfo: null }); expect(whMock).toHaveBeenCalledWith({ message: 'RunURL', value: { newTab: true, teacher: '-unknown-', url: 'http://www.google.com' } }); }); it('onRemoteExecute doesnt sendd the launch command to the extension when there is an invalid url', async () => { const ceh = new ClassroomEventHandler({} as WebViewDispatcher, {} as ScreenshotCaptureService); ceh.onRemoteExecute({ path: 'invalidurl', teacherInfo: null }); expect(whMock).not.toHaveBeenCalled(); }); it('onRemoteExecute will add the teacher info', async () => { const ceh = new ClassroomEventHandler({} as WebViewDispatcher, {} as ScreenshotCaptureService); ceh.onRemoteExecute({ path: 'http://www.google.com', teacherInfo: { guid: '1234', username: 'thanks' } }); expect(whMock).toHaveBeenCalledWith({ message: 'RunURL', value: { newTab: true, teacher: 'thanks', url: 'http://www.google.com' } }); }); });