"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = __importStar(require("react"));
const jest_dom_mocks_1 = require("@shopify/jest-dom-mocks");
const test_utilities_1 = require("../test-utilities");
const responsiveValue_1 = require("./responsiveValue");
describe('useResponsiveValue', () => {
    const spy = jest.fn();
    function Component({ values }) {
        const value = responsiveValue_1.useResponsiveValue(values);
        react_1.useEffect(() => {
            spy(value);
        });
        return null;
    }
    beforeEach(() => {
        jest_dom_mocks_1.matchMedia.mock();
    });
    afterEach(() => {
        jest_dom_mocks_1.matchMedia.restore();
        spy.mockReset();
    });
    it('returns undefined if no breakpoint matches', () => {
        const values = {
            small: 'small',
        };
        jest_dom_mocks_1.matchMedia.setMedia(() => ({
            matches: false,
        }));
        test_utilities_1.mountWithContext(<Component values={values}/>);
        expect(spy.mock.calls).toMatchObject([[undefined]]);
    });
    it('returns base value if it maches base breakpoint', () => {
        const values = {
            base: 'base',
        };
        jest_dom_mocks_1.matchMedia.setMedia((query) => ({
            matches: query === '(min-width: 0px) and (max-width: 749px)',
        }));
        test_utilities_1.mountWithContext(<Component values={values}/>);
        expect(spy.mock.calls).toMatchObject([[values.base]]);
    });
    it('returns small value if it maches small breakpoint', () => {
        const values = {
            small: 'small',
        };
        jest_dom_mocks_1.matchMedia.setMedia((query) => ({
            matches: query === '(min-width: 750px) and (max-width: 999px)',
        }));
        test_utilities_1.mountWithContext(<Component values={values}/>);
        expect(spy.mock.calls).toMatchObject([[values.small]]);
    });
    it('returns medium value if it maches medium breakpoint', () => {
        const values = {
            medium: 'medium',
        };
        jest_dom_mocks_1.matchMedia.setMedia((query) => ({
            matches: query === '(min-width: 1000px) and (max-width: 1199px)',
        }));
        test_utilities_1.mountWithContext(<Component values={values}/>);
        expect(spy.mock.calls).toMatchObject([[values.medium]]);
    });
    it('returns large value if it maches large breakpoint', () => {
        const values = {
            large: 'large',
        };
        jest_dom_mocks_1.matchMedia.setMedia((query) => ({
            matches: query === '(min-width: 1200px)',
        }));
        test_utilities_1.mountWithContext(<Component values={values}/>);
        expect(spy.mock.calls).toMatchObject([[values.large]]);
    });
    it('returns closest value if no breakpoint matches', () => {
        const values = {
            small: 'small',
            large: 'large',
        };
        jest_dom_mocks_1.matchMedia.setMedia((query) => ({
            matches: query === '(min-width: 750px) and (max-width: 999px)',
        }));
        test_utilities_1.mountWithContext(<Component values={values}/>);
        expect(spy.mock.calls).toMatchObject([[values.small]]);
    });
    it('returns closest non undefined value', () => {
        const values = {
            small: 'small',
            medium: null,
            large: undefined,
        };
        jest_dom_mocks_1.matchMedia.setMedia((query) => ({
            matches: query === '(min-width: 1200px)',
        }));
        test_utilities_1.mountWithContext(<Component values={values}/>);
        expect(spy.mock.calls).toMatchObject([[values.medium]]);
    });
});
describe('isResponsiveValue', () => {
    it('returns false when a value different of a responsive value is passed', () => {
        expect(responsiveValue_1.isResponsiveValue(undefined)).toBe(false);
        expect(responsiveValue_1.isResponsiveValue(null)).toBe(false);
        expect(responsiveValue_1.isResponsiveValue('some value')).toBe(false);
        expect(responsiveValue_1.isResponsiveValue({})).toBe(false);
        expect(responsiveValue_1.isResponsiveValue({ foo: 1, bar: 2 })).toBe(false);
        expect(responsiveValue_1.isResponsiveValue({ base: 1, small: 2, medium: 3, foo: 4 })).toBe(false);
    });
    it('returns true when responsive value is passed', () => {
        expect(responsiveValue_1.isResponsiveValue({
            small: 'some value',
        })).toBe(true);
        expect(responsiveValue_1.isResponsiveValue({
            base: 1,
            small: 2,
            medium: 3,
            large: 4,
        })).toBe(true);
    });
});
describe('maybeResponsiveToArray', () => {
    it('returns an array containing the passed non-responsive value as the value for the "base" breakpoint', () => {
        expect(responsiveValue_1.maybeResponsiveToArray('some value')).toStrictEqual([
            ['base', 'some value'],
        ]);
    });
    it('returns an array containing the entries of the responsive value as an array', () => {
        expect(new Set(responsiveValue_1.maybeResponsiveToArray({
            base: 1,
            small: 2,
            medium: 3,
            large: 4,
        }))).toStrictEqual(new Set([
            ['base', 1],
            ['small', 2],
            ['medium', 3],
            ['large', 4],
        ]));
        expect(new Set(responsiveValue_1.maybeResponsiveToArray({
            small: 2,
            medium: 3,
        }))).toStrictEqual(new Set([
            ['small', 2],
            ['medium', 3],
        ]));
    });
});
describe('useResponsiveStyle', () => {
    const spy = jest.fn();
    function Component({ config }) {
        const value = responsiveValue_1.useResponsiveStyle(config);
        react_1.useEffect(() => {
            spy(value);
        });
        return null;
    }
    beforeEach(() => {
        jest_dom_mocks_1.matchMedia.mock();
        jest_dom_mocks_1.matchMedia.setMedia(() => ({
            matches: true,
        }));
    });
    afterEach(() => {
        jest_dom_mocks_1.matchMedia.restore();
        spy.mockReset();
    });
    it('returns an empty object if passed value is undefined', () => {
        test_utilities_1.mountWithContext(<Component config={{
            color: { value: undefined },
        }}/>);
        expect(spy.mock.calls).toMatchObject([[{}]]);
    });
    it('returns a style object with the values', () => {
        test_utilities_1.mountWithContext(<Component config={{
            color: { value: 'red' },
            paddingLeft: { value: 1 },
        }}/>);
        expect(spy.mock.calls).toMatchObject([[{ color: 'red', paddingLeft: 1 }]]);
    });
    it('returns a style object with the responsive value', () => {
        test_utilities_1.mountWithContext(<Component config={{
            color: { value: { base: 'red' } },
            paddingLeft: { value: { base: 1 } },
        }}/>);
        expect(spy.mock.calls).toMatchObject([[{ color: 'red', paddingLeft: 1 }]]);
    });
    it('returns a style object with the transformed value', () => {
        test_utilities_1.mountWithContext(<Component config={{
            color: { value: 'red', transform: (value) => `dark${value}` },
            paddingLeft: { value: 1, transform: (value) => Number(value) },
        }}/>);
        expect(spy.mock.calls).toMatchObject([
            [{ color: 'darkred', paddingLeft: 1 }],
        ]);
    });
});
