This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const double = require('./double-1'); | |
describe('double', () => { | |
it('should create new array without mutating ref declared in outer scope', () => { | |
const a = [1, 2, 3]; | |
const b = double(a) | |
expect(a).toEqual([1, 2, 3]); | |
expect(b).toEqual([2, 4, 6]); | |
expect(a).not.toBe(b); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const double = arr => arr.map(item => item * 2); | |
module.exports = double; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const double = require('./double-0'); | |
describe('double', () => { | |
it('should modify array in place mutating ref declared in outer scope', () => { | |
const a = [1, 2, 3]; | |
const b = double(a); | |
expect(a).toEqual([2, 4, 6]); | |
expect(b).toEqual([2, 4, 6]); | |
expect(a).toBe(b); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const double = (arr) => { | |
arr.forEach((item, idx) => { | |
arr[idx] = item * 2; | |
}); | |
return arr; | |
}; | |
module.exports = double; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const reducer = require('./reducer-2'); | |
describe('reducer()', () => { | |
it('should not be affected by previous call', () => { | |
expect(reducer(undefined, { type: 'INCREMENT' })).toEqual({ count: 1 }); | |
expect(reducer(undefined, { type: 'INCREMENT' })).toEqual({ count: 1 }); | |
expect(reducer(undefined, { type: 'DECREMENT' })).toEqual({ count: -1 }); | |
expect(reducer({ count: 99 }, { type: 'INCREMENT' })) | |
.toEqual({ count: 100 }); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const reducer = (state = { count: 0 }, action) => { | |
switch (action.type) { | |
case 'INCREMENT': | |
return { count: state.count + 1 }; | |
case 'DECREMENT': | |
return { count: state.count - 1 }; | |
default: | |
return state; | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const reducer = require('./reducer-1'); | |
describe('reducer()', () => { | |
it('should not be affected by previous calls', () => { | |
expect(reducer(undefined, { type: 'INCREMENT' })).toEqual({ count: 1 }); | |
expect(reducer(undefined, { type: 'INCREMENT' })).toEqual({ count: 1 }); | |
expect(reducer(undefined, { type: 'DECREMENT' })).toEqual({ count: -1 }); | |
}); | |
it('should handle increment action', () => { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const reducer = (state = { count: 0 }, action) => { | |
switch (action.type) { | |
case 'INCREMENT': | |
state.count++; | |
break; | |
case 'DECREMENT': | |
state.count--; | |
break; | |
} | |
return state; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const reducer = require('./reducer-0'); | |
describe('reducer()', () => { | |
it('should handle increment and decrement actions', () => { | |
expect(reducer({ type: 'INCREMENT' })).toEqual({ count: 1 }); | |
expect(reducer({ type: 'INCREMENT' })).toEqual({ count: 2 }); | |
expect(reducer({ type: 'DECREMENT' })).toEqual({ count: 1 }); | |
}); | |
it('should depend on prevous invocations (history) - unpredictable!', () => { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const state = { count: 0 }; | |
const reducer = (action) => { | |
switch (action.type) { | |
case 'INCREMENT': | |
state.count++; | |
break; | |
case 'DECREMENT': | |
state.count--; | |
break; |