Skip to content
This repository has been archived by the owner on Jun 19, 2018. It is now read-only.

Add tests for utils #27

Merged
merged 2 commits into from
Feb 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions theme/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a fan of having 2 separate Babel configuration files. We should choose 1 and confine all babel-related config to it

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to keep the JS version, jestjs/jest#1468 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'm also not a fan of it. I considered moving everything to .babelrc, but it's pretty verbose that way. Could still be better than hacking jest, though.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whatever route you choose is just temporary until Babel 7, and then we have first-class support for .babelrc.js

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep 2 files instead of using a hack. More maintenance, but the maintenance burden is obvious.

Copy link
Contributor Author

@jimbo jimbo Feb 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's my instinct here as well, @zetlen. And @DrewML we should consolidate both files into .babelrc.js as soon as we're on 7.0 then.

"env": {
"test": {
"plugins": [
"transform-class-properties",
"transform-es2015-modules-commonjs",
"transform-object-rest-spread",
["transform-react-jsx", { "pragma": "createElement" }]
]
}
}
}
3 changes: 2 additions & 1 deletion theme/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 62 additions & 0 deletions theme/src/__tests__/utils.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { extract, fixedObserver, initObserver } from '../utils';

// mock a simple observer-type generator
function* arrayObserver() {
let array = [];

array.push(yield);
array.push(yield);

return array;
}

// mock dynamic imports
async function dynamicImport() {
return { foo: 'foo', default: 'bar' };
}

test('`extract` retrieves a named export', async () => {
const foo = await extract(dynamicImport(), 'foo');

expect(foo).toBe('foo');
});

test('`extract` retrieves the default export', async () => {
const bar = await extract(dynamicImport());

expect(bar).toBe('bar');
});

test('`extract` throws if the module fails to resolve', async () => {
const error = new Error('Invalid namespace object provided.');

expect(extract(null)).rejects.toEqual(error);
});

test('`extract` throws if the binding is not present', async () => {
const error = new Error('Binding baz not found.');

expect(extract(dynamicImport(), 'baz')).rejects.toEqual(error);
});

test('`fixedObserver` yields undefined', () => {
const gen = fixedObserver(2);

expect(gen.next()).toEqual({ value: void 0, done: false });
expect(gen.next()).toEqual({ value: void 0, done: false });
expect(gen.next()).toEqual({ value: void 0, done: true });
});

test('`fixedObserver` terminates if `length` is 0', () => {
const gen = fixedObserver(0);

expect(gen.next()).toEqual({ value: void 0, done: true });
expect(gen.next()).toEqual({ value: void 0, done: true });
});

test('`initObserver` starts an observer', () => {
const gen = initObserver(arrayObserver)();

expect(gen.next(0)).toEqual({ value: void 0, done: false });
expect(gen.next(1)).toEqual({ value: [0, 1], done: true });
});
21 changes: 14 additions & 7 deletions theme/src/utils.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
/**
* Extract a single export from a module.
* Retrieve a single exported binding from a module.
*
* @param {object} obj - A module's namespace object
* @param {string} name - The key to look up
* @param {string} name - The binding to retrieve
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're listing the params, would make sense to list the return (then VSCode/Webstorm will give proper hinting).

@returns {Promise<*>}

image

* @returns {Promise<*>}
*/
export const extract = (obj, name = 'default') =>
Promise.resolve(obj)
.then(mod => mod[name])
.catch(() => {
throw new Error(`Object is not a valid module.`);
});
Promise.resolve(obj).then(mod => {
if (!mod || typeof mod !== 'object') {
throw new Error('Invalid namespace object provided.');
}

if (!mod.hasOwnProperty(name)) {
throw new Error(`Binding ${name} not found.`);
}

return mod[name];
});

/**
* Create an observer-type generator that yields a fixed number of values.
Expand Down
2 changes: 1 addition & 1 deletion theme/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ module.exports = async env => {
runtimeCaching: [
{
urlPattern: new RegExp(mockImagesPath.href),
handler: 'cacheFirst'
handler: 'staleWhileRevalidate'
}
],

Expand Down