"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeTestRig = makeTestRig;
const core_1 = require("@vived/core");
const LabelDropRepo_1 = require("../Entities/LabelDropRepo");
const CorrectAnimationUC_1 = require("./CorrectAnimationUC");
function makeTestRig() {
    const appObjects = (0, core_1.makeAppObjectRepo)();
    const registerSingletonSpy = jest.spyOn(appObjects, "registerSingleton");
    const ao = appObjects.getOrCreate("LabelDrop");
    const labelDrop = (0, LabelDropRepo_1.makeLabelDropRepo)(ao);
    const uc = (0, CorrectAnimationUC_1.makeLabelDropCorrectAnimationUC)(ao);
    labelDrop.dragAlpha = 1;
    return { ao, registerSingletonSpy, uc, labelDrop, appObjects };
}
describe("Label Drop Correct Animation UC", () => {
    it("Registers as the singleton", () => {
        const { uc, registerSingletonSpy } = makeTestRig();
        expect(registerSingletonSpy).toHaveBeenCalledWith(uc);
    });
    it("Gets the UC", () => {
        const { uc, appObjects } = makeTestRig();
        expect(CorrectAnimationUC_1.LabelDropCorrectAnimationUC.get(appObjects)).toEqual(uc);
    });
    it("Sets is animating", () => {
        jest.useFakeTimers();
        const { uc } = makeTestRig();
        const totalAnimationTime = uc.fadeDurationMS + uc.feedbackDurationMS;
        expect(uc.getIsAnimating()).toEqual(false);
        let mockTime = 1000;
        Date.now = jest.fn().mockReturnValue(mockTime);
        uc.start();
        expect(uc.getIsAnimating()).toEqual(true);
        mockTime += totalAnimationTime - 1;
        Date.now = jest.fn().mockReturnValue(mockTime);
        jest.advanceTimersByTime(totalAnimationTime - 1);
        expect(uc.getIsAnimating()).toEqual(true);
        mockTime += 1;
        Date.now = jest.fn().mockReturnValue(mockTime);
        jest.advanceTimersByTime(1);
        expect(uc.getIsAnimating()).toEqual(false);
    });
    it("Fades the label after waiting for the feedback", () => {
        jest.useFakeTimers();
        const { uc, labelDrop } = makeTestRig();
        let mockTime = 1000;
        Date.now = jest.fn().mockReturnValue(mockTime);
        uc.start();
        expect(labelDrop.dragAlpha).toEqual(1);
        mockTime += uc.feedbackDurationMS;
        Date.now = jest.fn().mockReturnValue(mockTime);
        jest.advanceTimersByTime(uc.feedbackDurationMS);
        expect(labelDrop.dragAlpha).toEqual(1);
        mockTime += uc.fadeDurationMS * 0.5;
        Date.now = jest.fn().mockReturnValue(mockTime);
        jest.advanceTimersByTime(uc.fadeDurationMS * 0.5);
        expect(labelDrop.dragAlpha).toBeCloseTo(0.5);
        mockTime += uc.fadeDurationMS * 0.5 + 1;
        Date.now = jest.fn().mockReturnValue(mockTime);
        jest.advanceTimersByTime(uc.fadeDurationMS * 0.5);
        expect(labelDrop.dragAlpha).toEqual(0);
    });
    it("Stops the animation when cancelled", () => {
        jest.useFakeTimers();
        const { uc, labelDrop } = makeTestRig();
        let mockTime = 1000;
        Date.now = jest.fn().mockReturnValue(mockTime);
        uc.start();
        expect(labelDrop.dragAlpha).toEqual(1);
        uc.cancel();
        // Place the time in the middle of the fade
        mockTime += uc.feedbackDurationMS + uc.fadeDurationMS * 0.5;
        Date.now = jest.fn().mockReturnValue(mockTime);
        jest.advanceTimersByTime(uc.feedbackDurationMS);
        expect(labelDrop.dragAlpha).toEqual(1); // Should not fade
    });
    it("Returns true when the animation is completed as expected", (done) => {
        jest.useFakeTimers();
        const { uc } = makeTestRig();
        let mockTime = 1000;
        Date.now = jest.fn().mockReturnValue(mockTime);
        expect.assertions(1);
        // eslint-disable-next-line jest/valid-expect-in-promise
        uc.start().then((success) => {
            expect(success).toEqual(true);
            done();
        });
        mockTime += uc.feedbackDurationMS + uc.fadeDurationMS;
        Date.now = jest.fn().mockReturnValue(mockTime);
        jest.advanceTimersByTime(uc.feedbackDurationMS);
    });
    it("Returns false if the animation was cancelled", (done) => {
        jest.useFakeTimers();
        const { uc } = makeTestRig();
        const mockTime = 1000;
        Date.now = jest.fn().mockReturnValue(mockTime);
        expect.assertions(1);
        // eslint-disable-next-line jest/valid-expect-in-promise
        uc.start().then((success) => {
            expect(success).toEqual(false);
            done();
        });
        uc.cancel();
        jest.advanceTimersByTime(uc.feedbackDurationMS);
    });
});
//# sourceMappingURL=CorrectAnimation.test.js.map