|
| 1 | +jest.enableAutomock(); |
| 2 | +jest.dontMock('components/Area/Select/Area'); |
| 3 | +jest.dontMock('components/Error/Error'); |
| 4 | +jest.dontMock('react'); |
| 5 | +jest.dontMock('axios'); |
| 6 | +jest.dontMock('axios-mock-adapter'); |
| 7 | +jest.dontMock('enzyme'); |
| 8 | +jest.dontMock('services/Area'); |
| 9 | +jest.dontMock('services/Client'); |
| 10 | + |
| 11 | +describe('Test Select 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 | + |
| 27 | + it('Should show props on select element', (done) => { |
| 28 | + |
| 29 | + let Area = require('components/Area/Select/Area').default; |
| 30 | + let mockAdapter = new MockAdapter(axios); |
| 31 | + let component = mount( |
| 32 | + <Area className='Unknown' select={ { name : 'TestOne', id : 'TestTwo', className : 'class' } }/> |
| 33 | + ); |
| 34 | + |
| 35 | + mockAdapter.onGet(HOST + '/api/v1/area').reply(200, { areas : [] }) |
| 36 | + |
| 37 | + setTimeout(() => { |
| 38 | + |
| 39 | + try { |
| 40 | + expect( |
| 41 | + component.find('select').html() |
| 42 | + ).toEqual( |
| 43 | + '<select name="TestOne" id="TestTwo" class="class"></select>' |
| 44 | + ); |
| 45 | + expect(component.props()).toEqual({ |
| 46 | + className: 'Unknown', |
| 47 | + select: { |
| 48 | + name: 'TestOne', |
| 49 | + id: 'TestTwo', |
| 50 | + className: 'class' |
| 51 | + } |
| 52 | + }); |
| 53 | + done(); |
| 54 | + } catch(e) { |
| 55 | + console.log(e); |
| 56 | + } |
| 57 | + }, 0); |
| 58 | + }); |
| 59 | + |
| 60 | + |
| 61 | + it('Should show mocked data', (done) => { |
| 62 | + |
| 63 | + let Area = require('components/Area/Select/Area').default; |
| 64 | + let mockAdapter = new MockAdapter(axios); |
| 65 | + let component = mount( |
| 66 | + <Area /> |
| 67 | + ); |
| 68 | + let response = { |
| 69 | + areas : [ |
| 70 | + { |
| 71 | + _id : "Center", |
| 72 | + parent : "", |
| 73 | + __v : 0, |
| 74 | + ancestors : [] |
| 75 | + }, |
| 76 | + { |
| 77 | + _id : "South", |
| 78 | + parent : "", |
| 79 | + __v : 0, |
| 80 | + ancestors : [] |
| 81 | + } |
| 82 | + ] |
| 83 | + }; |
| 84 | + |
| 85 | + mockAdapter |
| 86 | + .onGet(HOST + '/api/v1/area').reply(200, response) |
| 87 | + |
| 88 | + setTimeout(() => { |
| 89 | + |
| 90 | + try { |
| 91 | + expect(component.find('option').at(0).text()).toEqual('Center'); |
| 92 | + expect(component.find('option').at(1).text()).toEqual('South'); |
| 93 | + done(); |
| 94 | + } catch(e) { |
| 95 | + console.log(e); |
| 96 | + } |
| 97 | + }, 0); |
| 98 | + }); |
| 99 | + |
| 100 | + it('Should show error message', (done) => { |
| 101 | + |
| 102 | + let response = { error:'Areas Not Found' }; |
| 103 | + let Area = require('components/Area/Select/Area').default; |
| 104 | + let mockAdapter = new MockAdapter(axios); |
| 105 | + let component = shallow( |
| 106 | + <Area /> |
| 107 | + ); |
| 108 | + |
| 109 | + mockAdapter.onGet(HOST + '/api/v1/area').reply(404, response); |
| 110 | + |
| 111 | + setTimeout(() => { |
| 112 | + |
| 113 | + try { |
| 114 | + component.update(); |
| 115 | + expect(component.render().text()).toEqual('Areas Not Found'); |
| 116 | + done(); |
| 117 | + } catch(e) { |
| 118 | + console.log(e); |
| 119 | + } |
| 120 | + }, 0); |
| 121 | + }); |
| 122 | + |
| 123 | + it('Should show default error message', (done) => { |
| 124 | + |
| 125 | + let mockAdapter = new MockAdapter(axios); |
| 126 | + let Area = require('components/Area/Select/Area').default; |
| 127 | + let component = shallow( |
| 128 | + <Area /> |
| 129 | + ); |
| 130 | + |
| 131 | + mockAdapter.onGet(HOST + '/api/v1/area').reply(503); |
| 132 | + |
| 133 | + setTimeout(() => { |
| 134 | + |
| 135 | + try { |
| 136 | + expect(component.render().text()).toEqual('Error Found: Trying get areas'); |
| 137 | + done(); |
| 138 | + } catch(e) { |
| 139 | + console.log(e); |
| 140 | + } |
| 141 | + }, 0); |
| 142 | + }); |
| 143 | +}); |
| 144 | + |
0 commit comments