Skip to content

Commit

Permalink
test(platform): Add spec watcher and unit tests
Browse files Browse the repository at this point in the history
- Add watch:tests for simpler dev / test
- Store utils spec
  • Loading branch information
bfricka authored and Brian Frichette committed Jul 18, 2017
1 parent 6982952 commit 24565e2
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 11 deletions.
81 changes: 81 additions & 0 deletions modules/store/spec/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/**
* @fileOverview
* @author Brian Frichette ([email protected])
*/

import { omit } from '../src/utils';
import { combineReducers, compose } from '@ngrx/store';

describe(`Store utils`, () => {
describe(`combineReducers()`, () => {
const s1 = { x: '' };
const s2 = { y: '' };
const r1 = (s = s1, a: any): typeof s1 =>
a.type === 's1' ? { ...s, x: a.payload } : s;
const r2 = (s = s2, a: any): typeof s2 =>
a.type === 's2' ? { ...s, y: a.payload } : s;
const reducers = { r1, r2, extraneous: true };
const initialState = { r1: { x: 'foo' }, r2: { y: 'bar' } };

let combination: any;

beforeEach(() => {
combination = combineReducers(reducers, initialState);
});

it(`should ignore extraneous keys`, () => {
expect(combination(undefined, { type: '' }).extraneous).toBeUndefined();
});

it(`should create a function that accepts state and action and returns combined state object`, () => {
const updateAction1 = { type: 's1', payload: 'baz' };
expect(combination(initialState, updateAction1)).toEqual({
...initialState,
r1: { x: updateAction1.payload },
});
});

it(`should handle initialState`, () => {
expect(combination(undefined, { type: '' })).toEqual(initialState);
});

it(`should return original state if nothing changed`, () => {
expect(combination(initialState, { type: '' })).toBe(initialState);
});
});

describe(`omit()`, () => {
let originalObj: { x: string; y: string; z?: string };

beforeEach(() => {
originalObj = { x: 'foo', y: 'bar' };
});

it(`should omit a key passed`, () => {
expect(omit(originalObj, 'x')).toEqual({ y: 'bar' });
});

it(`should not modify the original object`, () => {
expect(omit(originalObj, 'y')).not.toBe(originalObj);
});
});

describe(`compose()`, () => {
const cube = (n: number) => Math.pow(n, 3);
const precision = (n: number) => parseFloat(n.toPrecision(12));
const addPtTwo = (n: number) => n + 0.2;

it(`should should compose functions`, () => {
const addPrecision = compose(precision, addPtTwo);
const addPrecisionCubed = compose(cube, addPrecision);

expect(addPrecision(0.1)).toBe(0.3);
expect(addPrecisionCubed(0.1)).toBe(0.027);
});

it(`should act as identity if no functions passed`, () => {
const id = compose();
expect(id(1)).toBe(1);
});
});
});
12 changes: 9 additions & 3 deletions modules/store/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,15 @@ export function omit<T extends { [key: string]: any }>(
object: T,
keyToRemove: keyof T
): Partial<T> {
return Object.keys(object)
.filter(key => key !== keyToRemove)
.reduce((result, key) => Object.assign(result, { [key]: object[key] }), {});
const ret: Partial<T> = {};

for (const key of Object.keys(object)) {
if (key !== keyToRemove) {
ret[key] = object[key];
}
}

return ret;
}

export function compose<A>(): (i: A) => A;
Expand Down
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
"clean:ng-tools-ts": "rimraf \"./node_modules/@ngtools/webpack/node_modules/typescript\"",
"clean:ts": "yarn run clean:ng-cli-ts && yarn run clean:ng-tools-ts",
"cli": "ng",
"coverage:html": "nyc report --reporter=html",
"example:start": "yarn run cli -- serve",
"example:test": "yarn run cli -- test --code-coverage",
"ci": "npm run build && npm run test && nyc report --reporter=text-lcov | coveralls",
"ci": "yarn run build && yarn run test && nyc report --reporter=text-lcov | coveralls",
"prettier": "prettier --parser typescript --single-quote --trailing-comma --write \"./**/*.ts\"",
"watch:tests": "chokidar 'modules/**/*.ts' --initial -c 'nyc --reporter=text --reporter=html yarn run test:unit'",
"postinstall": "opencollective postinstall"
},
"keywords": [
Expand Down Expand Up @@ -61,6 +63,8 @@
"@types/node": "^7.0.5",
"@types/ora": "^0.3.31",
"@types/rimraf": "^0.0.28",
"chokidar": "^1.7.0",
"chokidar-cli": "^1.2.0",
"codelyzer": "^2.1.1",
"core-js": "^2.4.1",
"coveralls": "^2.13.0",
Expand Down Expand Up @@ -107,4 +111,4 @@
"url": "https://opencollective.com/ngrx",
"logo": "https://opencollective.com/opencollective/logo.txt"
}
}
}
64 changes: 58 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ any-promise@^1.0.0, any-promise@^1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/any-promise/-/any-promise-1.3.0.tgz#abc6afeedcea52e809cdc0376aed3ce39635d17f"

anymatch@^1.3.0:
anymatch@^1.1.0, anymatch@^1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/anymatch/-/anymatch-1.3.0.tgz#a3e52fa39168c825ff57b0248126ce5a8ff95507"
dependencies:
Expand Down Expand Up @@ -395,6 +395,10 @@ arr-flatten@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/arr-flatten/-/arr-flatten-1.0.1.tgz#e5ffe54d45e19f32f216e91eb99c8ce892bb604b"

array-filter@~0.0.0:
version "0.0.1"
resolved "https://registry.yarnpkg.com/array-filter/-/array-filter-0.0.1.tgz#7da8cf2e26628ed732803581fd21f67cacd2eeec"

array-find-index@^1.0.1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/array-find-index/-/array-find-index-1.0.2.tgz#df010aa1287e164bbda6f9723b0a96a1ec4187a1"
Expand All @@ -407,6 +411,14 @@ array-ify@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/array-ify/-/array-ify-1.0.0.tgz#9e528762b4a9066ad163a6962a364418e9626ece"

array-map@~0.0.0:
version "0.0.0"
resolved "https://registry.yarnpkg.com/array-map/-/array-map-0.0.0.tgz#88a2bab73d1cf7bcd5c1b118a003f66f665fa662"

array-reduce@~0.0.0:
version "0.0.0"
resolved "https://registry.yarnpkg.com/array-reduce/-/array-reduce-0.0.0.tgz#173899d3ffd1c7d9383e4479525dbe278cab5f2b"

array-slice@^0.2.3:
version "0.2.3"
resolved "https://registry.yarnpkg.com/array-slice/-/array-slice-0.2.3.tgz#dd3cfb80ed7973a75117cdac69b0b99ec86186f5"
Expand Down Expand Up @@ -651,6 +663,10 @@ [email protected]:
dependencies:
minimist "^1.2.0"

bluebird@^2.9.24:
version "2.11.0"
resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-2.11.0.tgz#534b9033c022c9579c56ba3b3e5a5caafbb650e1"

bluebird@^3.3.0, bluebird@^3.4.7:
version "3.5.0"
resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.5.0.tgz#791420d7f551eea2897453a8a77653f96606d67c"
Expand Down Expand Up @@ -845,7 +861,7 @@ camelcase@^1.0.2:
version "1.2.1"
resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-1.2.1.tgz#9bb5304d2e0b56698b2c758b08a3eaa9daa58a39"

camelcase@^2.0.0:
camelcase@^2.0.0, camelcase@^2.0.1:
version "2.1.1"
resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-2.1.1.tgz#7c1d16d679a1bbe59ca02cacecfb011e201f5a1f"

Expand Down Expand Up @@ -907,7 +923,18 @@ chalk@^2.0.0, chalk@^2.0.1:
escape-string-regexp "^1.0.5"
supports-color "^4.0.0"

chokidar@^1.4.1, chokidar@^1.4.3, chokidar@^1.6.0:
chokidar-cli@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/chokidar-cli/-/chokidar-cli-1.2.0.tgz#8e7f58442273182018be1868e53c22af65a21948"
dependencies:
anymatch "^1.1.0"
bluebird "^2.9.24"
chokidar "^1.0.1"
lodash "^3.7.0"
shell-quote "^1.4.3"
yargs "^3.7.2"

chokidar@^1.0.1, chokidar@^1.4.1, chokidar@^1.4.3, chokidar@^1.6.0, chokidar@^1.7.0:
version "1.7.0"
resolved "https://registry.yarnpkg.com/chokidar/-/chokidar-1.7.0.tgz#798e689778151c8076b4b360e5edd28cda2bb468"
dependencies:
Expand Down Expand Up @@ -971,7 +998,7 @@ cliui@^2.1.0:
right-align "^0.1.1"
wordwrap "0.0.2"

cliui@^3.2.0:
cliui@^3.0.3, cliui@^3.2.0:
version "3.2.0"
resolved "https://registry.yarnpkg.com/cliui/-/cliui-3.2.0.tgz#120601537a916d29940f934da3b48d585a39213d"
dependencies:
Expand Down Expand Up @@ -3654,7 +3681,7 @@ lodash.uniq@^4.5.0:
version "4.5.0"
resolved "https://registry.yarnpkg.com/lodash.uniq/-/lodash.uniq-4.5.0.tgz#d0225373aeb652adc1bc82e4945339a842754773"

lodash@^3.8.0:
lodash@^3.7.0, lodash@^3.8.0:
version "3.10.1"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-3.10.1.tgz#5bf45e8e49ba4189e17d482789dfd15bd140b7b6"

Expand Down Expand Up @@ -5406,6 +5433,15 @@ shebang-regex@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/shebang-regex/-/shebang-regex-1.0.0.tgz#da42f49740c0b42db2ca9728571cb190c98efea3"

shell-quote@^1.4.3:
version "1.6.1"
resolved "https://registry.yarnpkg.com/shell-quote/-/shell-quote-1.6.1.tgz#f4781949cce402697127430ea3b3c5476f481767"
dependencies:
array-filter "~0.0.0"
array-map "~0.0.0"
array-reduce "~0.0.0"
jsonify "~0.0.0"

signal-exit@^2.0.0:
version "2.1.2"
resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-2.1.2.tgz#375879b1f92ebc3b334480d038dc546a6d558564"
Expand Down Expand Up @@ -6399,6 +6435,10 @@ [email protected]:
version "0.1.0"
resolved "https://registry.yarnpkg.com/window-size/-/window-size-0.1.0.tgz#5438cd2ea93b202efa3a19fe8887aee7c94f9c9d"

window-size@^0.1.4:
version "0.1.4"
resolved "https://registry.yarnpkg.com/window-size/-/window-size-0.1.4.tgz#f8e1aa1ee5a53ec5bf151ffa09742a6ad7697876"

[email protected], wordwrap@~0.0.2:
version "0.0.2"
resolved "https://registry.yarnpkg.com/wordwrap/-/wordwrap-0.0.2.tgz#b79669bb42ecb409f83d583cad52ca17eaa1643f"
Expand Down Expand Up @@ -6509,7 +6549,7 @@ xtend@^4.0.0, xtend@~4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/xtend/-/xtend-4.0.1.tgz#a5c6d532be656e23db820efb943a1f04998d63af"

y18n@^3.2.1:
y18n@^3.2.0, y18n@^3.2.1:
version "3.2.1"
resolved "https://registry.yarnpkg.com/y18n/-/y18n-3.2.1.tgz#6d15fba884c08679c0d77e88e7759e811e07fa41"

Expand All @@ -6529,6 +6569,18 @@ yargs-parser@^5.0.0:
dependencies:
camelcase "^3.0.0"

yargs@^3.7.2:
version "3.32.0"
resolved "https://registry.yarnpkg.com/yargs/-/yargs-3.32.0.tgz#03088e9ebf9e756b69751611d2a5ef591482c995"
dependencies:
camelcase "^2.0.1"
cliui "^3.0.3"
decamelize "^1.1.1"
os-locale "^1.4.0"
string-width "^1.0.1"
window-size "^0.1.4"
y18n "^3.2.0"

yargs@^6.0.0, yargs@^6.6.0:
version "6.6.0"
resolved "https://registry.yarnpkg.com/yargs/-/yargs-6.6.0.tgz#782ec21ef403345f830a808ca3d513af56065208"
Expand Down

0 comments on commit 24565e2

Please sign in to comment.