Skip to content

Commit

Permalink
fix(operators-js): Fix _location and _media tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
SamTolmay committed May 28, 2022
1 parent 90abaef commit f47bede
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 50 deletions.
Expand Up @@ -32,7 +32,7 @@ const validProperties = [
];

function _location({ arrayIndices, basePath, home, location, pageId, params, window }) {
if (!window || !window.location) {
if (!window?.location) {
throw new Error(
`Operator Error: Browser window.location not available for _location. Received: ${JSON.stringify(
params
Expand Down
Expand Up @@ -20,6 +20,20 @@ jest.mock('@lowdefy/operators', () => ({
getFromObject: jest.fn(),
}));

const window = {
location: {
hash: 'window.location.hash',
host: 'window.location.host',
hostname: 'window.location.hostname',
href: 'window.location.href',
origin: 'window.location.origin',
pathname: 'window.location.pathname',
port: 'window.location.port',
protocol: 'window.location.protocol',
search: 'window.location.search',
},
};

const input = {
arrayIndices: [0],
basePath: 'base',
Expand All @@ -29,25 +43,12 @@ const input = {
location: 'location',
pageId: 'page-id',
params: 'origin',
window: {
location: {
hash: 'window.location.hash',
host: 'window.location.host',
hostname: 'window.location.hostname',
href: 'window.location.href',
origin: 'window.location.origin',
pathname: 'window.location.pathname',
port: 'window.location.port',
protocol: 'window.location.protocol',
search: 'window.location.search',
},
},
lowdefyGlobal: { lowdefyGlobal: true },
};

test('location calls getFromObject', async () => {
const lowdefyOperators = await import('@lowdefy/operators');
location(input);
location({ ...input, window });
expect(lowdefyOperators.getFromObject.mock.calls).toEqual([
[
{
Expand All @@ -74,19 +75,20 @@ test('location calls getFromObject', async () => {
]);
});

test('_location throw on no window', () => {
expect(() => location(input)).toThrow(
'Operator Error: Browser window.location not available for _location. Received: "origin" at location.'
);
});

test('_location throw on no location', () => {
Object.defineProperty(window, 'location', {
writable: true,
configurable: true,
value: undefined,
});
expect(() => location({ params: 'origin', location: 'locationId' })).toThrow(
'Operator Error: Browser window.location not available for _location. Received: "origin" at locationId.'
expect(() => location({ ...input, window: {} })).toThrow(
'Operator Error: Browser window.location not available for _location. Received: "origin" at location.'
);
});

test('_location throw invalid param', () => {
expect(() => location({ ...input, params: 'none' })).toThrow(
'Operator Error: _location only returns values for basePath, hash, homePageId, host, hostname, href, origin, pageId, pathname, port, protocol, search. Received: "none" at location.'
expect(() => location({ ...input, window, params: 'invalid' })).toThrow(
'Operator Error: _location only returns values for basePath, hash, homePageId, host, hostname, href, origin, pageId, pathname, port, protocol, search. Received: "invalid" at location.'
);
});
Expand Up @@ -24,8 +24,8 @@ const breakpoints = {
xl: 1600,
};

function _media({ arrayIndices, location, params }) {
if (!window || !window.innerWidth) {
function _media({ arrayIndices, location, params, window }) {
if (!window?.innerWidth) {
throw new Error(
`Operator Error: device window width not available for _media. Received: ${JSON.stringify(
params
Expand Down
Expand Up @@ -19,48 +19,59 @@ import media from './media.js';

console.error = () => {};

beforeEach(() => {
Object.defineProperty(window, 'innerHeight', { writable: true, configurable: true, value: 300 });
Object.defineProperty(window, 'innerWidth', { writable: true, configurable: true, value: 500 });
});
const window = { innerHeight: 300, innerWidth: 500 };

test('_media full media object', () => {
expect(media({ params: true, location: 'locationId' })).toEqual({
expect(media({ params: true, location: 'locationId', window })).toEqual({
height: 300,
size: 'xs',
width: 500,
});
});

test('_media size', () => {
expect(media({ params: 'size', location: 'locationId' })).toEqual('xs');
Object.defineProperty(window, 'innerWidth', { writable: true, configurable: true, value: 700 });
expect(media({ params: 'size', location: 'locationId' })).toEqual('sm');
Object.defineProperty(window, 'innerWidth', { writable: true, configurable: true, value: 900 });
expect(media({ params: 'size', location: 'locationId' })).toEqual('md');
Object.defineProperty(window, 'innerWidth', { writable: true, configurable: true, value: 1100 });
expect(media({ params: 'size', location: 'locationId' })).toEqual('lg');
Object.defineProperty(window, 'innerWidth', { writable: true, configurable: true, value: 1400 });
expect(media({ params: 'size', location: 'locationId' })).toEqual('xl');
Object.defineProperty(window, 'innerWidth', { writable: true, configurable: true, value: 1900 });
expect(media({ params: 'size', location: 'locationId' })).toEqual('xxl');
expect(
media({ params: 'size', location: 'locationId', window: { innerHeight: 300, innerWidth: 500 } })
).toEqual('xs');
expect(
media({ params: 'size', location: 'locationId', window: { innerHeight: 300, innerWidth: 700 } })
).toEqual('sm');
expect(
media({ params: 'size', location: 'locationId', window: { innerHeight: 300, innerWidth: 900 } })
).toEqual('md');
expect(
media({
params: 'size',
location: 'locationId',
window: { innerHeight: 300, innerWidth: 1100 },
})
).toEqual('lg');
expect(
media({
params: 'size',
location: 'locationId',
window: { innerHeight: 300, innerWidth: 1400 },
})
).toEqual('xl');
expect(
media({
params: 'size',
location: 'locationId',
window: { innerHeight: 300, innerWidth: 1900 },
})
).toEqual('xxl');
});

test('_media width', () => {
expect(media({ params: 'width', location: 'locationId' })).toEqual(500);
expect(media({ params: 'width', location: 'locationId', window })).toEqual(500);
});

test('_media height', () => {
expect(media({ params: 'height', location: 'locationId' })).toEqual(300);
expect(media({ params: 'height', location: 'locationId', window })).toEqual(300);
});

test('_media throw on no innerWidth', () => {
Object.defineProperty(window, 'innerWidth', {
writable: true,
configurable: true,
value: undefined,
});
expect(() => media({ params: true, location: 'locationId' })).toThrow(
expect(() => media({ params: true, location: 'locationId', window: {} })).toThrow(
'Operator Error: device window width not available for _media. Received: true at locationId.'
);
});

0 comments on commit f47bede

Please sign in to comment.