/**
 * @jest-environment jsdom
 */
import ELearning from "..";
import CfCore from "../../../core/CfCore"
import Navigation from "../../Navigation"
import { ContentBlock } from "../../Navigation/typings";
import { ModuleAction
    , ExamAction, 
    ExamProperties} from "../typings";
import { 
    ELearningTypes, 
    QuestionAction 
} from "../typings";

let windowSpy
let mockSender;
let mockImpressionManager = {};

describe('ELearning', () => {
    beforeAll(() => {
        mockSender = {
            add() { return false }
        }
    })

    beforeEach(() => {
        windowSpy = jest.spyOn(window, "window", "get");
    })

    afterEach(() => {
        // to remove the singleton instance
        (CfCore as any).instance = null
    })

    it('Should deliver a "Module" event', () => {
        const senderSpy = jest.spyOn(mockSender, 'add')
        const bsCore = CfCore.createInstance(mockSender as any, mockImpressionManager as any, false, ContentBlock.Core,'device-id', false)

        const event = {
            id: 'id',
            progress: 78,
            action: ModuleAction.View
        }

        ELearning.logModuleEvent(event)

        expect(senderSpy).toBeCalledWith(
            expect.anything(),
            expect.anything(),
            expect.objectContaining(
            {
                type: ELearningTypes.Module,
                props: event
            }),
            expect.anything())
    })


    it('Should deliver a "Exam" event', () => {
        const senderSpy = jest.spyOn(mockSender, 'add')
        const bsCore = CfCore.createInstance(mockSender as any, mockImpressionManager as any, false, ContentBlock.Core, 'device-id', false)

        const event = {
            id: 'id',
            action: ExamAction.Start,
        }

        ELearning.logExamEvent(event as ExamProperties)

        expect(senderSpy).toBeCalledWith(
            expect.anything(),
            expect.anything(),
            expect.objectContaining(
            {
                type: ELearningTypes.Exam,
                props: event
            }),
            expect.anything())
    })

    it('Should deliver a "Question" event', () => {
        const senderSpy = jest.spyOn(mockSender, 'add')
        const bsCore = CfCore.createInstance(mockSender as any, mockImpressionManager as any, false, ContentBlock.Core, 'device-id', false)

        const event = {
            id: 'id',
            exam_id: '123',
            answer_id: 'id2',
            action:QuestionAction.Answer
        }

        ELearning.logQuestionEvent(event)

        expect(senderSpy).toBeCalledWith(
            expect.anything(),
            expect.anything(),
            expect.objectContaining(
            {
                type: ELearningTypes.Question,
                props: event
            }),
            expect.anything())
    })


})