Created
January 28, 2016 00:16
-
-
Save anonymous/56aeb64f79c23318575a to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/bijeso
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
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <meta charset="utf-8"> | |
| <meta name="viewport" content="width=device-width"> | |
| <title>JS Bin</title> | |
| <script src="https://wzrd.in/standalone/expect@latest"></script> | |
| <script src="https://wzrd.in/standalone/deep-freeze@latest"></script> | |
| </head> | |
| <body> | |
| <script id="jsbin-javascript"> | |
| const filter = (state, action) => { | |
| switch (action.type) { | |
| case 'SET_FILTER_TIME': | |
| return Object.assign({}, state, { | |
| time: action.time | |
| }); | |
| case 'SET_FILTER_DAY': | |
| return Object.assign({}, state, { | |
| day: action.day | |
| }); | |
| case 'ADD_FILTER_GAME': | |
| return Object.assign({}, state, { | |
| games: [...state.games, action.game] | |
| }); | |
| case 'REMOVE_FILTER_GAME': | |
| const games = state.games; | |
| const gameIndex = games.indexOf(action.game); | |
| return Object.assign({}, state, { | |
| games: [ | |
| ...games.slice(0, gameIndex), | |
| ...games.slice(gameIndex + 1) | |
| ] | |
| }); | |
| default: | |
| return state; | |
| } | |
| }; | |
| const testFilterTime = () => { | |
| const filterBefore = { | |
| time: null, | |
| day: null, | |
| games: [] | |
| }; | |
| const filterAfter = { | |
| time: { | |
| from: [18, 0], | |
| to: [21, 0] | |
| }, | |
| day: null, | |
| games: [] | |
| }; | |
| deepFreeze(filterBefore); | |
| expect( | |
| filter(filterBefore, { | |
| type: 'SET_FILTER_TIME', | |
| time: { | |
| from: [18, 0], | |
| to: [21, 0] | |
| } | |
| }) | |
| ).toEqual(filterAfter); | |
| }; | |
| testFilterTime(); | |
| const testFilterDay = () => { | |
| const filterBefore = { | |
| time: null, | |
| day: null, | |
| games: [] | |
| }; | |
| const filterAfter = { | |
| time: null, | |
| day: 2, | |
| games: [] | |
| }; | |
| deepFreeze(filterBefore); | |
| expect( | |
| filter(filterBefore, { | |
| type: 'SET_FILTER_DAY', | |
| day: 2 | |
| }) | |
| ).toEqual(filterAfter); | |
| }; | |
| testFilterDay(); | |
| const testFilterAddGame = () => { | |
| const filterBefore = { | |
| time: null, | |
| day: null, | |
| games: [] | |
| }; | |
| const filterAfter = { | |
| time: null, | |
| day: null, | |
| games: ['Имаджинариум'] | |
| }; | |
| deepFreeze(filterBefore); | |
| expect( | |
| filter(filterBefore, { | |
| type: 'ADD_FILTER_GAME', | |
| game: 'Имаджинариум' | |
| }) | |
| ).toEqual(filterAfter); | |
| }; | |
| testFilterAddGame(); | |
| const testFilterRemoveGame = () => { | |
| const filterBefore = { | |
| time: null, | |
| day: null, | |
| games: ['Имаджинариум','Экивоки (II издание)','Шакал: Остров Сокровищ'] | |
| }; | |
| const filterAfter = { | |
| time: null, | |
| day: null, | |
| games: ['Имаджинариум','Экивоки (II издание)'] | |
| }; | |
| deepFreeze(filterBefore); | |
| expect( | |
| filter(filterBefore, { | |
| type: 'REMOVE_FILTER_GAME', | |
| game: 'Шакал: Остров Сокровищ' | |
| }) | |
| ).toEqual(filterAfter); | |
| }; | |
| testFilterRemoveGame(); | |
| console.log('All good!'); | |
| </script> | |
| <script id="jsbin-source-javascript" type="text/javascript">const filter = (state, action) => { | |
| switch (action.type) { | |
| case 'SET_FILTER_TIME': | |
| return Object.assign({}, state, { | |
| time: action.time | |
| }); | |
| case 'SET_FILTER_DAY': | |
| return Object.assign({}, state, { | |
| day: action.day | |
| }); | |
| case 'ADD_FILTER_GAME': | |
| return Object.assign({}, state, { | |
| games: [...state.games, action.game] | |
| }); | |
| case 'REMOVE_FILTER_GAME': | |
| const games = state.games; | |
| const gameIndex = games.indexOf(action.game); | |
| return Object.assign({}, state, { | |
| games: [ | |
| ...games.slice(0, gameIndex), | |
| ...games.slice(gameIndex + 1) | |
| ] | |
| }); | |
| default: | |
| return state; | |
| } | |
| }; | |
| const testFilterTime = () => { | |
| const filterBefore = { | |
| time: null, | |
| day: null, | |
| games: [] | |
| }; | |
| const filterAfter = { | |
| time: { | |
| from: [18, 0], | |
| to: [21, 0] | |
| }, | |
| day: null, | |
| games: [] | |
| }; | |
| deepFreeze(filterBefore); | |
| expect( | |
| filter(filterBefore, { | |
| type: 'SET_FILTER_TIME', | |
| time: { | |
| from: [18, 0], | |
| to: [21, 0] | |
| } | |
| }) | |
| ).toEqual(filterAfter); | |
| }; | |
| testFilterTime(); | |
| const testFilterDay = () => { | |
| const filterBefore = { | |
| time: null, | |
| day: null, | |
| games: [] | |
| }; | |
| const filterAfter = { | |
| time: null, | |
| day: 2, | |
| games: [] | |
| }; | |
| deepFreeze(filterBefore); | |
| expect( | |
| filter(filterBefore, { | |
| type: 'SET_FILTER_DAY', | |
| day: 2 | |
| }) | |
| ).toEqual(filterAfter); | |
| }; | |
| testFilterDay(); | |
| const testFilterAddGame = () => { | |
| const filterBefore = { | |
| time: null, | |
| day: null, | |
| games: [] | |
| }; | |
| const filterAfter = { | |
| time: null, | |
| day: null, | |
| games: ['Имаджинариум'] | |
| }; | |
| deepFreeze(filterBefore); | |
| expect( | |
| filter(filterBefore, { | |
| type: 'ADD_FILTER_GAME', | |
| game: 'Имаджинариум' | |
| }) | |
| ).toEqual(filterAfter); | |
| }; | |
| testFilterAddGame(); | |
| const testFilterRemoveGame = () => { | |
| const filterBefore = { | |
| time: null, | |
| day: null, | |
| games: ['Имаджинариум','Экивоки (II издание)','Шакал: Остров Сокровищ'] | |
| }; | |
| const filterAfter = { | |
| time: null, | |
| day: null, | |
| games: ['Имаджинариум','Экивоки (II издание)'] | |
| }; | |
| deepFreeze(filterBefore); | |
| expect( | |
| filter(filterBefore, { | |
| type: 'REMOVE_FILTER_GAME', | |
| game: 'Шакал: Остров Сокровищ' | |
| }) | |
| ).toEqual(filterAfter); | |
| }; | |
| testFilterRemoveGame(); | |
| console.log('All good!');</script></body> | |
| </html> |
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 filter = (state, action) => { | |
| switch (action.type) { | |
| case 'SET_FILTER_TIME': | |
| return Object.assign({}, state, { | |
| time: action.time | |
| }); | |
| case 'SET_FILTER_DAY': | |
| return Object.assign({}, state, { | |
| day: action.day | |
| }); | |
| case 'ADD_FILTER_GAME': | |
| return Object.assign({}, state, { | |
| games: [...state.games, action.game] | |
| }); | |
| case 'REMOVE_FILTER_GAME': | |
| const games = state.games; | |
| const gameIndex = games.indexOf(action.game); | |
| return Object.assign({}, state, { | |
| games: [ | |
| ...games.slice(0, gameIndex), | |
| ...games.slice(gameIndex + 1) | |
| ] | |
| }); | |
| default: | |
| return state; | |
| } | |
| }; | |
| const testFilterTime = () => { | |
| const filterBefore = { | |
| time: null, | |
| day: null, | |
| games: [] | |
| }; | |
| const filterAfter = { | |
| time: { | |
| from: [18, 0], | |
| to: [21, 0] | |
| }, | |
| day: null, | |
| games: [] | |
| }; | |
| deepFreeze(filterBefore); | |
| expect( | |
| filter(filterBefore, { | |
| type: 'SET_FILTER_TIME', | |
| time: { | |
| from: [18, 0], | |
| to: [21, 0] | |
| } | |
| }) | |
| ).toEqual(filterAfter); | |
| }; | |
| testFilterTime(); | |
| const testFilterDay = () => { | |
| const filterBefore = { | |
| time: null, | |
| day: null, | |
| games: [] | |
| }; | |
| const filterAfter = { | |
| time: null, | |
| day: 2, | |
| games: [] | |
| }; | |
| deepFreeze(filterBefore); | |
| expect( | |
| filter(filterBefore, { | |
| type: 'SET_FILTER_DAY', | |
| day: 2 | |
| }) | |
| ).toEqual(filterAfter); | |
| }; | |
| testFilterDay(); | |
| const testFilterAddGame = () => { | |
| const filterBefore = { | |
| time: null, | |
| day: null, | |
| games: [] | |
| }; | |
| const filterAfter = { | |
| time: null, | |
| day: null, | |
| games: ['Имаджинариум'] | |
| }; | |
| deepFreeze(filterBefore); | |
| expect( | |
| filter(filterBefore, { | |
| type: 'ADD_FILTER_GAME', | |
| game: 'Имаджинариум' | |
| }) | |
| ).toEqual(filterAfter); | |
| }; | |
| testFilterAddGame(); | |
| const testFilterRemoveGame = () => { | |
| const filterBefore = { | |
| time: null, | |
| day: null, | |
| games: ['Имаджинариум','Экивоки (II издание)','Шакал: Остров Сокровищ'] | |
| }; | |
| const filterAfter = { | |
| time: null, | |
| day: null, | |
| games: ['Имаджинариум','Экивоки (II издание)'] | |
| }; | |
| deepFreeze(filterBefore); | |
| expect( | |
| filter(filterBefore, { | |
| type: 'REMOVE_FILTER_GAME', | |
| game: 'Шакал: Остров Сокровищ' | |
| }) | |
| ).toEqual(filterAfter); | |
| }; | |
| testFilterRemoveGame(); | |
| console.log('All good!'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment