ADD unit tests for refs.getSourceType

This commit is contained in:
Will Farley 2020-04-14 03:15:45 -07:00
parent 544572d40a
commit 563445550e

View File

@ -0,0 +1,41 @@
import { getSourceType } from '../modules/refs';
jest.mock('global', () => {
const globalMock = {};
// Change global.location value to handle edge cases
// Add additional variations of global.location mock return values in this array.
// NOTE: The order must match the order that global.location is called in the unit tests.
const edgecaseLocations = [
{ origin: 'https://storybook.js.org', pathname: '/storybook/index.html' },
];
// global.location value after all edgecaseLocations are returned
const lastLocation = { origin: 'https://storybook.js.org', pathname: '/storybook/' };
Object.defineProperties(globalMock, {
location: {
get: edgecaseLocations
.reduce((mockFn, location) => mockFn.mockReturnValueOnce(location), jest.fn())
.mockReturnValue(lastLocation),
},
});
return globalMock;
});
describe('refs', () => {
describe('getSourceType(source)', () => {
// These tests must be run first and in correct order.
// The order matches the "edgecaseLocations" order in the 'global' mock function above.
describe('edge cases', () => {
it('returns "local" when source matches location with /index.html in path', () => {
// mockReturnValue(edgecaseLocations[0])
expect(getSourceType('https://storybook.js.org/storybook/iframe.html')).toBe('local');
});
});
// Other tests use "lastLocation" for the 'global' mock
it('returns "local" when source matches location', () => {
expect(getSourceType('https://storybook.js.org/storybook/iframe.html')).toBe('local');
});
it('returns "external" when source does not match location', () => {
expect(getSourceType('https://external.com/storybook/iframe.html')).toBe('external');
});
});
});