"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeTestRig = makeTestRig;
const core_1 = require("@vived/core");
const core_2 = require("@vived/core");
const LabelDropRepo_1 = require("../Entities/LabelDropRepo");
const IncorrectAnimationUC_1 = require("./IncorrectAnimationUC");
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, IncorrectAnimationUC_1.makeLabelDropIncorrectAnimationUC)(ao);
    labelDrop.dragAlpha = 1;
    return { ao, registerSingletonSpy, uc, labelDrop, appObjects };
}
describe("Label Drop Incorrect Animation UC", () => {
    it("Registers as the singleton", () => {
        const { uc, registerSingletonSpy } = makeTestRig();
        expect(registerSingletonSpy).toHaveBeenCalledWith(uc);
    });
    it("Gets the UC", () => {
        const { uc, appObjects } = makeTestRig();
        expect(IncorrectAnimationUC_1.LabelDropIncorrectAnimationUC.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).toBeLessThan(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("Moves the label after waiting for feedback", () => {
        jest.useFakeTimers();
        const { uc, labelDrop } = makeTestRig();
        labelDrop.dragLabelTransform.worldPosition = new core_2.Vector3(1, 2, 3);
        uc.desiredFinalPosition = new core_2.Vector3(10, 20, 30);
        let mockTime = 1000;
        Date.now = jest.fn().mockReturnValue(mockTime);
        uc.start();
        expect(labelDrop.dragLabelTransform.worldPosition).toEqual(new core_2.Vector3(1, 2, 3));
        mockTime += uc.feedbackDurationMS;
        Date.now = jest.fn().mockReturnValue(mockTime);
        jest.advanceTimersByTime(uc.feedbackDurationMS);
        expect(labelDrop.dragLabelTransform.worldPosition).toEqual(new core_2.Vector3(1, 2, 3));
        mockTime += uc.fadeDurationMS * 0.5;
        Date.now = jest.fn().mockReturnValue(mockTime);
        jest.advanceTimersByTime(uc.fadeDurationMS * 0.5);
        expect(labelDrop.dragLabelTransform.worldPosition.x).toBeGreaterThan(1);
        expect(labelDrop.dragLabelTransform.worldPosition.x).toBeLessThan(10);
        expect(labelDrop.dragLabelTransform.worldPosition.y).toBeGreaterThan(2);
        expect(labelDrop.dragLabelTransform.worldPosition.y).toBeLessThan(20);
        expect(labelDrop.dragLabelTransform.worldPosition.z).toBeGreaterThan(3);
        expect(labelDrop.dragLabelTransform.worldPosition.z).toBeLessThan(30);
        mockTime += uc.fadeDurationMS * 0.5 + 1;
        Date.now = jest.fn().mockReturnValue(mockTime);
        jest.advanceTimersByTime(uc.fadeDurationMS * 0.5);
        expect(labelDrop.dragLabelTransform.worldPosition).toEqual(new core_2.Vector3(10, 20, 30));
    });
    it("Stops the animation when cancelled", () => {
        jest.useFakeTimers();
        const { uc, labelDrop } = makeTestRig();
        labelDrop.dragLabelTransform.worldPosition = new core_2.Vector3(1, 2, 3);
        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);
        expect(labelDrop.dragLabelTransform.worldPosition).toEqual(new core_2.Vector3(1, 2, 3));
    });
    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=IncorrectAnimationUC.test.js.map