diff --git a/exercises/00 - Setup Jest/ReadMe.md b/exercises/00 - Setup Jest/ReadMe.md index db2125f..b6da339 100644 --- a/exercises/00 - Setup Jest/ReadMe.md +++ b/exercises/00 - Setup Jest/ReadMe.md @@ -2,4 +2,9 @@ For this exercise the function to test and the test have already been written. -Your task is to setup jest. To pass you should be able to run `npm test` or `yarn test` and see a successful test output. \ No newline at end of file +Your task is to setup jest. To pass you should be able to run `npm test` or `yarn test` and see a successful test output. + +# Solution + + 1) Add `jest-cli` as devDependency + 2) Update test script to simple call `jest` diff --git a/exercises/00 - Setup Jest/package.json b/exercises/00 - Setup Jest/package.json index 6b9d0fe..5d45684 100644 --- a/exercises/00 - Setup Jest/package.json +++ b/exercises/00 - Setup Jest/package.json @@ -3,6 +3,9 @@ "private": true, "version": "0.0.1", "scripts": { - "test": "echo \"This is part of the jest setup\" && exit 1" + "test": "jest" + }, + "devDependencies": { + "jest-cli": "^19.0.2" } } diff --git a/exercises/10 - Lookup/index.spec.js b/exercises/10 - Lookup/index.spec.js index 20636c3..dd26af3 100644 --- a/exercises/10 - Lookup/index.spec.js +++ b/exercises/10 - Lookup/index.spec.js @@ -29,6 +29,14 @@ const users = [ // lookup() const lookup = (login, property) => { // START -- THIS IS WHERE YOUR CODE GOES + const user = users.find(u=> u.login === login); + if (!user) { + throw new Error("Could not find user"); + } + if (!user.hasOwnProperty(property)) { + throw new Error("Could not find property"); + } + return user[property]; // END }; diff --git a/exercises/11 - Pure Function/index.spec.js b/exercises/11 - Pure Function/index.spec.js index ace432a..1a00b66 100644 --- a/exercises/11 - Pure Function/index.spec.js +++ b/exercises/11 - Pure Function/index.spec.js @@ -1,11 +1,13 @@ // setPrice(item: Object, price: Number) => item: Object const setPrice = (item, price) => { - // TODO: implement + return Object.assign({}, item, { + price, + }); }; // addToCart(cart: Array, item: Object) => cart: Array const addToCart = (cart, item) => { - // TODO: implement + return cart.concat(item); }; describe('setPrice()', () => { diff --git a/exercises/12 - Object Snapshot/index.spec.js b/exercises/12 - Object Snapshot/index.spec.js index 954dcc3..10c54b2 100644 --- a/exercises/12 - Object Snapshot/index.spec.js +++ b/exercises/12 - Object Snapshot/index.spec.js @@ -1,7 +1,21 @@ import { createStore, combineReducers } from 'redux'; -const usersReducer = (state, action) => { - // Implement this reducer to pass the tests below +const usersReducer = (state = [], action) => { + switch (action.type) { + case 'SAVE_USER': + const { user } = action; + if (state.some(u => u.handle === user.handle)) { + return state.map(u => { + if (u.handle === user.handle) { + return Object.assign({}, u, user); + } + return u; + }); + } + return [...state, user]; + default: + return state; + } }; const configureStore = (initialState = {}) => { diff --git a/exercises/13 - Component Testing/index.js b/exercises/13 - Component Testing/index.js index 28038d8..bc4b68d 100644 --- a/exercises/13 - Component Testing/index.js +++ b/exercises/13 - Component Testing/index.js @@ -1,5 +1,19 @@ import React from 'react'; +import classnames from 'classnames'; -export default () => { - // Implement this component to pass the tests in ./__tests/index.spec.js +export default ({data, onClick}) => { + const handleClick = (key) => () => { + return onClick(key); + } + return (); }; diff --git a/exercises/14 - Promise Testing/index.js b/exercises/14 - Promise Testing/index.js index e7b3ce3..8230210 100644 --- a/exercises/14 - Promise Testing/index.js +++ b/exercises/14 - Promise Testing/index.js @@ -23,15 +23,41 @@ export const defaultFetchHeaders = { }; export const camelCase = input => { - // TODO: implement + return (typeof input === 'string') ? input.charAt(0).toLowerCase() + input.slice(1) : input; }; -const normalizeCasing = value => { - // TODO: implement +export const normalizeCasing = value => { + if (value === null) { + return null; + } + if (Array.isArray(value)) { + return value.map(normalizeCasing); + } + if (typeof value === 'object') { + return Object.keys(value).reduce((acc, curr) => { + return { + ...acc, + [camelCase(curr)]: normalizeCasing(value[curr]), + }; + }, {}); + } + return value; }; const callApi = (url = '', options = {}) => { - // TODO: implement + const apiUrl = (/https?:\/\//.test(url)) ? url : `${LOCATION_ORIGIN}${url}`; + const fetchOptions = Object.assign({}, defaultFetchHeaders, options); + return fetch(apiUrl, fetchOptions) + .then((resp) => { + if (resp.status !== 204) { + return resp.json() + .then((json) => { + const results = { json: normalizeCasing(json), resp }; + return (resp.status >= 500 && resp.status < 600) ? Promise.reject(results) : results; + }); + } + return { json: null, resp }; + }); }; export default callApi; diff --git a/exercises/14 - Promise Testing/index.spec.js b/exercises/14 - Promise Testing/index.spec.js index de33b57..77015f9 100644 --- a/exercises/14 - Promise Testing/index.spec.js +++ b/exercises/14 - Promise Testing/index.spec.js @@ -1,5 +1,5 @@ import 'isomorphic-fetch'; -import callApi, { defaultFetchHeaders, camelCase } from './index'; +import callApi, { defaultFetchHeaders, normalizeCasing } from './index'; var mockFetch; jest.mock('node-fetch', () => { @@ -125,35 +125,35 @@ describe('callApi', () => { }); }); -describe('#camelCase', () => { +describe('#normalizeCasing', () => { const obj = { SuperLongCSharpVariableNameThatIsInMyJSON: 1 }; const formattedObj = { superLongCSharpVariableNameThatIsInMyJSON: 1 }; it('returns the input if it is an array with no objects', () => { - expect(camelCase([1, 2, 3])).toEqual([1,2,3]); + expect(normalizeCasing([1, 2, 3])).toEqual([1,2,3]); }); it('returns the input if it is a nested array with no objects', () => { - expect(camelCase([1, [2], 3])).toEqual([1, [2] ,3]); + expect(normalizeCasing([1, [2], 3])).toEqual([1, [2] ,3]); }); it('camel-cases object keys when a single object is nested inside an array', () => { - expect(camelCase([obj])).toEqual([formattedObj]); + expect(normalizeCasing([obj])).toEqual([formattedObj]); }); it('camel-cases object keys when an object and a primitive are nested in an array', () => { - expect(camelCase([obj, 1])).toEqual([formattedObj, 1]); + expect(normalizeCasing([obj, 1])).toEqual([formattedObj, 1]); }); it('camel-cases object keys if the object contains an array', () => { const nestedArray = Object.assign({}, obj, { Arr: [1, 2, 3] }); const expected = Object.assign({}, formattedObj, { arr: [1, 2, 3] }); - expect(camelCase(nestedArray)).toEqual(expected); + expect(normalizeCasing(nestedArray)).toEqual(expected); }); it('camel-cases object keys if the object contains an array containing an object', () => { const nestedArray = Object.assign({}, obj, { Arr: [1, { B: 2 }, 3] }); const expected = Object.assign({}, formattedObj, { arr: [1, { b: 2 }, 3] }); - expect(camelCase(nestedArray)).toEqual(expected); + expect(normalizeCasing(nestedArray)).toEqual(expected); }); }); diff --git a/exercises/14 - Promise Testing/package.json b/exercises/14 - Promise Testing/package.json index 0fee23b..4005acf 100644 --- a/exercises/14 - Promise Testing/package.json +++ b/exercises/14 - Promise Testing/package.json @@ -19,8 +19,5 @@ "latest", "stage-2" ] - }, - "jest": { - "testEnvironment": "node" } } \ No newline at end of file diff --git a/exercises/15 - Expect Extensions/index.spec.js b/exercises/15 - Expect Extensions/index.spec.js index e7e5e28..4f24d49 100644 --- a/exercises/15 - Expect Extensions/index.spec.js +++ b/exercises/15 - Expect Extensions/index.spec.js @@ -28,20 +28,20 @@ expect.extend({ }); test('should return function', () => { - const value = undefined; + const value = ()=>({}); expect(value).toBeFunction(); }); test('should not be function', () => { - const value = undefined; + const value = null; expect(value).not.toBeFunction(); }); test('should return property', () => { - const value = undefined; + const value = {name: 'Kyle Welch', age: 27}; expect(value).toHaveProperty('name'); }); test('should return property', () => { - const value = undefined; + const value = {name: 'Kyle Welch'}; expect(value).not.toHaveProperty('age'); });