|
| 1 | +jest.enableAutomock(); |
| 2 | +jest.dontMock('components/Area/Create/Area'); |
| 3 | +jest.dontMock('components/Area/Select/Area'); |
| 4 | +jest.dontMock('components/Error/Error'); |
| 5 | +jest.dontMock('react'); |
| 6 | +jest.dontMock('axios'); |
| 7 | +jest.dontMock('axios-mock-adapter'); |
| 8 | +jest.dontMock('enzyme'); |
| 9 | +jest.dontMock('services/Area'); |
| 10 | + |
| 11 | +describe('Test Create Area', () => { |
| 12 | + require('../tests/__mocks__/LocalStorageMock'); |
| 13 | + |
| 14 | + const React = require('react'); |
| 15 | + const Enzyme = require('enzyme'); |
| 16 | + const shallow = Enzyme.shallow; |
| 17 | + const mount = Enzyme.mount; |
| 18 | + const context = { |
| 19 | + router: { |
| 20 | + push: (arg) => arg |
| 21 | + } |
| 22 | + }; |
| 23 | + |
| 24 | + let axios = require('axios'); |
| 25 | + let MockAdapter = require('axios-mock-adapter'); |
| 26 | + let response; |
| 27 | + |
| 28 | + it('Should show default error when fails to create area', (done) => { |
| 29 | + |
| 30 | + let areaInput; |
| 31 | + let areaSelected; |
| 32 | + let Area = require('components/Area/Create/Area').default; |
| 33 | + let mockAdapter = new MockAdapter(axios); |
| 34 | + let context = { |
| 35 | + router: { |
| 36 | + push: (arg) => { |
| 37 | + expect(arg).toEqual('/clients'); |
| 38 | + } |
| 39 | + } |
| 40 | + }; |
| 41 | + let component = mount( |
| 42 | + <Area />, |
| 43 | + { context } |
| 44 | + ); |
| 45 | + let expected = { |
| 46 | + _id : 'Test', |
| 47 | + parent : 'South', |
| 48 | + } |
| 49 | + let response = { |
| 50 | + areas : [ |
| 51 | + { |
| 52 | + _id : "Center", |
| 53 | + }, |
| 54 | + { |
| 55 | + _id : "South", |
| 56 | + } |
| 57 | + ] |
| 58 | + }; |
| 59 | + |
| 60 | + mockAdapter |
| 61 | + .onGet(HOST + '/api/v1/area').reply(200, response) |
| 62 | + .onPost(HOST + '/api/v1/area').reply(503, { error : 'Error on server'}) |
| 63 | + |
| 64 | + setTimeout(() => { |
| 65 | + |
| 66 | + try { |
| 67 | + |
| 68 | + let inputs = { |
| 69 | + area : component.find('input'), |
| 70 | + parent : component.find('select'), |
| 71 | + } |
| 72 | + |
| 73 | + areaInput = component.find('input'); |
| 74 | + areaInput.node.value = 'Test'; |
| 75 | + areaInput.simulate('change', areaInput); |
| 76 | + |
| 77 | + areaSelected = component.find('select'); |
| 78 | + areaSelected.node.value = 'South'; |
| 79 | + areaSelected.simulate('change', areaSelected); |
| 80 | + |
| 81 | + component.find('form').simulate('submit', { target: component.find('form').get(0) }); |
| 82 | + } catch (e) { |
| 83 | + console.log(e); |
| 84 | + } |
| 85 | + }, 5); |
| 86 | + |
| 87 | + |
| 88 | + setTimeout(() => { |
| 89 | + |
| 90 | + try { |
| 91 | + |
| 92 | + expect(component.state().area).toEqual(expected); |
| 93 | + expect(component.state().error).toEqual('Error on server'); |
| 94 | + done(); |
| 95 | + } catch (e) { |
| 96 | + console.log(e); |
| 97 | + } |
| 98 | + }, 25); |
| 99 | + }); |
| 100 | + |
| 101 | + it('Should show default error when fails to create area', (done) => { |
| 102 | + |
| 103 | + let areaInput; |
| 104 | + let areaSelected; |
| 105 | + let Area = require('components/Area/Create/Area').default; |
| 106 | + let mockAdapter = new MockAdapter(axios); |
| 107 | + let context = { |
| 108 | + router: { |
| 109 | + push: (arg) => { |
| 110 | + expect(arg).toEqual('/clients'); |
| 111 | + } |
| 112 | + } |
| 113 | + }; |
| 114 | + let component = mount( |
| 115 | + <Area />, |
| 116 | + { context } |
| 117 | + ); |
| 118 | + let expected = { |
| 119 | + _id : 'Test', |
| 120 | + parent : 'South', |
| 121 | + } |
| 122 | + let response = { |
| 123 | + areas : [ |
| 124 | + { |
| 125 | + _id : "Center", |
| 126 | + }, |
| 127 | + { |
| 128 | + _id : "South", |
| 129 | + } |
| 130 | + ] |
| 131 | + }; |
| 132 | + |
| 133 | + mockAdapter |
| 134 | + .onGet(HOST + '/api/v1/area').reply(200, response) |
| 135 | + .onPost(HOST + '/api/v1/area').reply(503) |
| 136 | + |
| 137 | + setTimeout(() => { |
| 138 | + |
| 139 | + try { |
| 140 | + |
| 141 | + let inputs = { |
| 142 | + area : component.find('input'), |
| 143 | + parent : component.find('select'), |
| 144 | + } |
| 145 | + |
| 146 | + areaInput = component.find('input'); |
| 147 | + areaInput.node.value = 'Test'; |
| 148 | + areaInput.simulate('change', areaInput); |
| 149 | + |
| 150 | + areaSelected = component.find('select'); |
| 151 | + areaSelected.node.value = 'South'; |
| 152 | + areaSelected.simulate('change', areaSelected); |
| 153 | + |
| 154 | + component.find('form').simulate('submit', { target: component.find('form').get(0) }); |
| 155 | + } catch (e) { |
| 156 | + console.log(e); |
| 157 | + } |
| 158 | + }, 5); |
| 159 | + |
| 160 | + |
| 161 | + setTimeout(() => { |
| 162 | + |
| 163 | + try { |
| 164 | + |
| 165 | + expect(component.state().area).toEqual(expected); |
| 166 | + expect(component.state().error).toEqual('Error trying create area'); |
| 167 | + done(); |
| 168 | + } catch (e) { |
| 169 | + console.log(e); |
| 170 | + } |
| 171 | + }, 25); |
| 172 | + }); |
| 173 | + |
| 174 | + it('Should return data on submit', (done) => { |
| 175 | + |
| 176 | + let areaInput; |
| 177 | + let areaSelected; |
| 178 | + let Area = require('components/Area/Create/Area').default; |
| 179 | + let mockAdapter = new MockAdapter(axios); |
| 180 | + let context = { |
| 181 | + router: { |
| 182 | + push: (arg) => { |
| 183 | + expect(arg).toEqual('/clients'); |
| 184 | + } |
| 185 | + } |
| 186 | + }; |
| 187 | + let component = mount( |
| 188 | + <Area />, |
| 189 | + { context } |
| 190 | + ); |
| 191 | + let expected = { |
| 192 | + _id : 'Test', |
| 193 | + parent : 'South', |
| 194 | + } |
| 195 | + let response = { |
| 196 | + areas : [ |
| 197 | + { |
| 198 | + _id : "Center", |
| 199 | + }, |
| 200 | + { |
| 201 | + _id : "South", |
| 202 | + } |
| 203 | + ] |
| 204 | + }; |
| 205 | + |
| 206 | + mockAdapter |
| 207 | + .onGet(HOST + '/api/v1/area').reply(200, response) |
| 208 | + .onPost(HOST + '/api/v1/area').reply(201) |
| 209 | + |
| 210 | + setTimeout(() => { |
| 211 | + |
| 212 | + try { |
| 213 | + |
| 214 | + let inputs = { |
| 215 | + area : component.find('input'), |
| 216 | + parent : component.find('select'), |
| 217 | + } |
| 218 | + |
| 219 | + areaInput = component.find('input'); |
| 220 | + areaInput.node.value = 'Test'; |
| 221 | + areaInput.simulate('change', areaInput); |
| 222 | + |
| 223 | + areaSelected = component.find('select'); |
| 224 | + areaSelected.node.value = 'South'; |
| 225 | + areaSelected.simulate('change', areaSelected); |
| 226 | + |
| 227 | + component.find('form').simulate('submit', { target: component.find('form').get(0) }); |
| 228 | + |
| 229 | + expect(component.state().area).toEqual(expected); |
| 230 | + expect(component.state().error).toEqual(''); |
| 231 | + done(); |
| 232 | + } catch (e) { |
| 233 | + console.log(e); |
| 234 | + } |
| 235 | + }, 25); |
| 236 | + }); |
| 237 | + |
| 238 | + it('Should show default error message', (done) => { |
| 239 | + |
| 240 | + let mockAdapter = new MockAdapter(axios); |
| 241 | + let Area = require('components/Area/Create/Area').default; |
| 242 | + let component = shallow( |
| 243 | + <Area />, |
| 244 | + { context } |
| 245 | + ); |
| 246 | + |
| 247 | + setTimeout(() => { |
| 248 | + |
| 249 | + try { |
| 250 | + expect(component.find('h1').text()).toEqual('Create Area'); |
| 251 | + expect(component.find('input').hasClass('input')).toBeTruthy(); |
| 252 | + expect(component.find('button').text()).toEqual('Save'); |
| 253 | + done(); |
| 254 | + } catch(e) { |
| 255 | + console.log(e); |
| 256 | + } |
| 257 | + }, 0); |
| 258 | + }); |
| 259 | +}); |
| 260 | + |
0 commit comments