Skip to content

Commit

Permalink
test custom functions
Browse files Browse the repository at this point in the history
  • Loading branch information
dortzur committed May 29, 2018
1 parent b3c8723 commit 20f4d47
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 19 deletions.
20 changes: 7 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"name": "renorm",
"version": "0.3.8",
"description": "Avoid React avoidable re-renders with Redux, Reselect and Normalizr using an optimisied selector.",
"description":
"Avoid React avoidable re-renders with Redux, Reselect and Normalizr using an optimisied selector.",
"main": "lib/index.js",
"scripts": {
"build": "webpack",
Expand All @@ -13,16 +14,11 @@
"test": "cross-env BABEL_ENV=commonjs jest",
"test:watch": "npm test -- --watch",
"test:cov": "npm test -- --coverage",
"test:ci": "jest --coverage --ci --testResultsProcessor=jest-junit | coveralls",
"test:ci":
"jest --coverage --ci --testResultsProcessor=jest-junit | coveralls",
"clean": "rimraf lib dist es coverage"
},
"files": [
"dist",
"lib",
"es",
"src",
"index.d.ts"
],
"files": ["dist", "lib", "es", "src", "index.d.ts"],
"keywords": [
"normalizr",
"reselect",
Expand All @@ -36,6 +32,7 @@
"author": "Dor Tzur",
"license": "MIT",
"devDependencies": {
"humps": "^2.0.1",
"babel-core": "^6.26.3",
"babel-jest": "^22.4.3",
"babel-loader": "^7.1.0",
Expand All @@ -53,10 +50,7 @@
"bugs": "https://github.com/dortzur/renorm/issues",
"jest": {
"testRegex": "(.spec.js)$",
"coverageReporters": [
"text-lcov",
"lcov"
]
"coverageReporters": ["text-lcov", "lcov"]
},
"sideEffects": false,
"dependencies": {
Expand Down
75 changes: 75 additions & 0 deletions src/__snapshots__/index.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,78 @@ Object {
"testIssue": "N",
}
`;

exports[`renorm uses a process function 1`] = `
Object {
"company_name": "Apple Inc.",
"financial_status": "N",
"id": "COMP_AAPL",
"market_category": "Q",
"round_lot_size": 100,
"security_name": "Apple Inc. - Common Stock",
"stock": Object {
"change": -7.08,
"field4": "",
"id": "AAPL",
"last_earnings_report": Object {
"earnings": 77598,
"quarter": 3,
"report_id": "AAPL_QUARTER_3",
},
"name": "Apple Inc",
"percent_change": "-4.10%",
"price": 734,
"quarter_earnings": Array [
Object {
"earnings": 91507,
"quarter": 1,
"report_id": "AAPL_QUARTER_1",
},
Object {
"earnings": 69667,
"quarter": 2,
"report_id": "AAPL_QUARTER_2",
},
Object {
"earnings": 77598,
"quarter": 3,
"report_id": "AAPL_QUARTER_3",
},
],
},
"test_issue": "N",
}
`;

exports[`renorm uses a process function 2`] = `
Object {
"change": -7.08,
"field4": "",
"id": "AAPL",
"last_earnings_report": Object {
"earnings": 77598,
"quarter": 3,
"report_id": "AAPL_QUARTER_3",
},
"name": "Apple Inc",
"percent_change": "-4.10%",
"price": 734,
"quarter_earnings": Array [
Object {
"earnings": 91507,
"quarter": 1,
"report_id": "AAPL_QUARTER_1",
},
Object {
"earnings": 69667,
"quarter": 2,
"report_id": "AAPL_QUARTER_2",
},
Object {
"earnings": 77598,
"quarter": 3,
"report_id": "AAPL_QUARTER_3",
},
],
}
`;
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const defaultOptions = {
* @param options {{entitiesPath: string, process: function}}
* @return {function}
*/
const renorm = (inputSelector, schema, options = {}) => {
const renorm = (inputSelector, schema, options = defaultOptions) => {
const createEntitySelector = schemaSelectorCreator(schema);
const entityNames = getEntityNames(schema);
options = Object.assign({}, defaultOptions, options);
Expand Down
42 changes: 37 additions & 5 deletions src/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { normalize } from 'normalizr';
import stockFixture from '../fixtures/stock-fixure';
import { Schemas } from '../fixtures/schema-fixture';
import produce from 'immer';

import { decamelizeKeys } from 'humps';
const getStocks = (state) => state.stocks;

let state = null;
Expand Down Expand Up @@ -77,8 +77,40 @@ describe('renorm', () => {
getAppleStock(newState);
expect(getAppleStock.recomputations()).toEqual(2);
});
// it('uses custom options',()=>{
// const getStocksSelector = renorm(getStocks, Schemas.COMPANY_ARRAY,{});
//
// })

it('uses a process function', () => {
const getStocksSelector = renorm(getStocks, Schemas.COMPANY_ARRAY, {
process: (result) => decamelizeKeys(result),
});
const result = getStocksSelector(state);
expect(result[1]).toEqual(
expect.objectContaining({
company_name: 'Apple Inc.',
financial_status: 'N',
id: 'COMP_AAPL',
market_category: 'Q',
round_lot_size: 100,
security_name: 'Apple Inc. - Common Stock',
})
);
expect(result[1]).toMatchSnapshot();

const getAppleStock = renorm(() => 'AAPL', Schemas.STOCK, {
process: (result) => decamelizeKeys(result),
});
const appleStock = getAppleStock(state);
expect(appleStock).toMatchSnapshot();
expect(appleStock).toEqual(
expect.objectContaining({
change: -7.08,
field4: '',
id: 'AAPL',
last_earnings_report: {
earnings: 77598,
quarter: 3,
report_id: 'AAPL_QUARTER_3',
},
})
);
});
});
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1988,6 +1988,10 @@ https-browserify@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/https-browserify/-/https-browserify-1.0.0.tgz#ec06c10e0a34c0f2faf199f7fd7fc78fffd03c73"

humps@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/humps/-/humps-2.0.1.tgz#dd02ea6081bd0568dc5d073184463957ba9ef9aa"

iconv-lite@0.4.19:
version "0.4.19"
resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.19.tgz#f7468f60135f5e5dad3399c0a81be9a1603a082b"
Expand Down

0 comments on commit 20f4d47

Please sign in to comment.