Skip to content

Instantly share code, notes, and snippets.

View lupomontero's full-sized avatar

Lupo Montero lupomontero

View GitHub Profile
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);
});
const double = arr => arr.map(item => item * 2);
module.exports = double;
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);
});
const double = (arr) => {
arr.forEach((item, idx) => {
arr[idx] = item * 2;
});
return arr;
};
module.exports = double;
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 });
});
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;
}
};
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', () => {
const reducer = (state = { count: 0 }, action) => {
switch (action.type) {
case 'INCREMENT':
state.count++;
break;
case 'DECREMENT':
state.count--;
break;
}
return state;
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!', () => {
const state = { count: 0 };
const reducer = (action) => {
switch (action.type) {
case 'INCREMENT':
state.count++;
break;
case 'DECREMENT':
state.count--;
break;