|
| 1 | +import fetchMock from 'fetch-mock'; |
| 2 | +import createStore from '../src/store'; |
| 3 | + |
| 4 | +import * as actions from '../src/actions'; |
| 5 | + |
| 6 | +describe('actions', () => { |
| 7 | + it('should create an action to set recent snippets', () => { |
| 8 | + const snippets = [ |
| 9 | + { |
| 10 | + id: 1, |
| 11 | + content: 'test', |
| 12 | + syntax: 'JavaScript', |
| 13 | + }, |
| 14 | + { |
| 15 | + id: 2, |
| 16 | + content: 'batman', |
| 17 | + syntax: 'Python', |
| 18 | + }, |
| 19 | + ]; |
| 20 | + const store = createStore(); |
| 21 | + store.dispatch(actions.setRecentSnippets(snippets)); |
| 22 | + |
| 23 | + expect(store.getState().toJS()).toEqual({ |
| 24 | + recent: [1, 2], |
| 25 | + snippets: { |
| 26 | + 1: { |
| 27 | + id: 1, |
| 28 | + content: 'test', |
| 29 | + syntax: 'JavaScript', |
| 30 | + }, |
| 31 | + 2: { |
| 32 | + id: 2, |
| 33 | + content: 'batman', |
| 34 | + syntax: 'Python', |
| 35 | + }, |
| 36 | + }, |
| 37 | + syntaxes: [], |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should create an action to fetch recent snippets', async () => { |
| 42 | + const snippets = [ |
| 43 | + { |
| 44 | + id: 1, |
| 45 | + content: 'test', |
| 46 | + syntax: 'JavaScript', |
| 47 | + }, |
| 48 | + { |
| 49 | + id: 2, |
| 50 | + content: 'batman', |
| 51 | + syntax: 'Python', |
| 52 | + }, |
| 53 | + ]; |
| 54 | + |
| 55 | + fetchMock.getOnce('http://api.xsnippet.org/snippets', JSON.stringify(snippets)); |
| 56 | + |
| 57 | + const store = createStore(); |
| 58 | + await store.dispatch(actions.fetchRecentSnippets); |
| 59 | + |
| 60 | + expect(store.getState().toJS()).toEqual({ |
| 61 | + recent: [1, 2], |
| 62 | + snippets: { |
| 63 | + 1: { |
| 64 | + id: 1, |
| 65 | + content: 'test', |
| 66 | + syntax: 'JavaScript', |
| 67 | + }, |
| 68 | + 2: { |
| 69 | + id: 2, |
| 70 | + content: 'batman', |
| 71 | + syntax: 'Python', |
| 72 | + }, |
| 73 | + }, |
| 74 | + syntaxes: [], |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should create an action to set snippet', () => { |
| 79 | + const snippet = { |
| 80 | + id: 3, |
| 81 | + content: 'not batman', |
| 82 | + syntax: 'Go', |
| 83 | + }; |
| 84 | + const store = createStore(); |
| 85 | + store.dispatch(actions.setSnippet(snippet)); |
| 86 | + |
| 87 | + expect(store.getState().toJS()).toEqual({ |
| 88 | + recent: [], |
| 89 | + snippets: { |
| 90 | + 3: { |
| 91 | + id: 3, |
| 92 | + content: 'not batman', |
| 93 | + syntax: 'Go', |
| 94 | + }, |
| 95 | + }, |
| 96 | + syntaxes: [], |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + it('should create an action to fetch snippet', async () => { |
| 101 | + const snippet = { |
| 102 | + id: 3, |
| 103 | + content: 'not batman', |
| 104 | + syntax: 'Go', |
| 105 | + }; |
| 106 | + |
| 107 | + fetchMock.getOnce(`http://api.xsnippet.org/snippets/${snippet.id}`, JSON.stringify(snippet)); |
| 108 | + |
| 109 | + const store = createStore(); |
| 110 | + await store.dispatch(actions.fetchSnippet(snippet.id)); |
| 111 | + |
| 112 | + expect(store.getState().toJS()).toEqual({ |
| 113 | + recent: [], |
| 114 | + snippets: { |
| 115 | + 3: { |
| 116 | + id: 3, |
| 117 | + content: 'not batman', |
| 118 | + syntax: 'Go', |
| 119 | + }, |
| 120 | + }, |
| 121 | + syntaxes: [], |
| 122 | + }); |
| 123 | + }); |
| 124 | + |
| 125 | + it('should create an action to set syntaxes', () => { |
| 126 | + const syntaxes = ['JavaScript', 'Python', 'Java', 'Go', 'Plain Text']; |
| 127 | + const store = createStore(); |
| 128 | + store.dispatch(actions.setSyntaxes(syntaxes)); |
| 129 | + |
| 130 | + expect(store.getState().toJS()).toEqual({ |
| 131 | + recent: [], |
| 132 | + snippets: {}, |
| 133 | + syntaxes, |
| 134 | + }); |
| 135 | + }); |
| 136 | + |
| 137 | + it('should create an action to fetch syntaxes', async () => { |
| 138 | + const syntaxes = ['JavaScript', 'Python', 'Java', 'Go', 'Plain Text']; |
| 139 | + |
| 140 | + fetchMock.getOnce('http://api.xsnippet.org/syntaxes', JSON.stringify(syntaxes)); |
| 141 | + |
| 142 | + const store = createStore(); |
| 143 | + await store.dispatch(actions.fetchSyntaxes); |
| 144 | + |
| 145 | + expect(store.getState().toJS()).toEqual({ |
| 146 | + recent: [], |
| 147 | + snippets: {}, |
| 148 | + syntaxes, |
| 149 | + }); |
| 150 | + }); |
| 151 | + |
| 152 | + it('should create an action to post snippet', async () => { |
| 153 | + const snippet = { |
| 154 | + id: 4, |
| 155 | + content: 'Batman', |
| 156 | + syntax: 'JavaScript', |
| 157 | + }; |
| 158 | + |
| 159 | + fetchMock.postOnce('http://api.xsnippet.org/snippets', JSON.stringify(snippet)); |
| 160 | + |
| 161 | + const store = createStore(); |
| 162 | + await store.dispatch(actions.postSnippet(snippet, () => {})); |
| 163 | + |
| 164 | + expect(store.getState().toJS()).toEqual({ |
| 165 | + recent: [], |
| 166 | + snippets: { |
| 167 | + 4: { |
| 168 | + id: 4, |
| 169 | + content: 'Batman', |
| 170 | + syntax: 'JavaScript', |
| 171 | + }, |
| 172 | + }, |
| 173 | + syntaxes: [], |
| 174 | + }); |
| 175 | + }); |
| 176 | +}); |
0 commit comments