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

Commit

Permalink
Write tests for utils
Browse files Browse the repository at this point in the history
  • Loading branch information
jimbo committed Feb 7, 2018
1 parent 95fbeca commit d572047
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 8 deletions.
12 changes: 12 additions & 0 deletions theme/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"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
* @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

0 comments on commit d572047

Please sign in to comment.