|
| 1 | +jest.enableAutomock(); |
| 2 | +jest.dontMock('components/Visit/Show/Visit'); |
| 3 | +jest.dontMock('components/Error/Error'); |
| 4 | +jest.dontMock('react'); |
| 5 | +jest.dontMock('react-router'); |
| 6 | +jest.dontMock('axios'); |
| 7 | +jest.dontMock('axios-mock-adapter'); |
| 8 | +jest.dontMock('enzyme'); |
| 9 | +jest.dontMock('services/Visit'); |
| 10 | +jest.dontMock('helpers/DateHelper'); |
| 11 | + |
| 12 | +describe('Test Visit', () => { |
| 13 | + require('../tests/__mocks__/LocalStorageMock'); |
| 14 | + |
| 15 | + const React = require('react'); |
| 16 | + const Enzyme = require('enzyme'); |
| 17 | + const shallow = Enzyme.shallow; |
| 18 | + const mount = Enzyme.mount; |
| 19 | + |
| 20 | + let axios = require('axios'); |
| 21 | + let MockAdapter = require('axios-mock-adapter'); |
| 22 | + |
| 23 | + it('Visit should show mocked data', (done) => { |
| 24 | + |
| 25 | + let id = '123abc'; |
| 26 | + let response = { |
| 27 | + visit: [{ |
| 28 | + visit_date: "2017-10-01", |
| 29 | + value_received: 300, |
| 30 | + sales_quantity: 150, |
| 31 | + client: { |
| 32 | + id: id, |
| 33 | + name: 'Jon Snow', |
| 34 | + address: '7 Street', |
| 35 | + city: 'Winterfell', |
| 36 | + ability: 200, |
| 37 | + frequency: 10, |
| 38 | + area: { |
| 39 | + _id: 'Center', |
| 40 | + parents: 'Center' |
| 41 | + } |
| 42 | + }, |
| 43 | + }] |
| 44 | + }; |
| 45 | + let Visit; |
| 46 | + let component; |
| 47 | + let mockAdapter = new MockAdapter(axios); |
| 48 | + |
| 49 | + mockAdapter.onGet(HOST + '/api/v1/visit/' + id).reply(200, response); |
| 50 | + |
| 51 | + Visit = require('components/Visit/Show/Visit').default; |
| 52 | + |
| 53 | + component = mount( |
| 54 | + <Visit params={ { id: id} }/> |
| 55 | + ); |
| 56 | + |
| 57 | + setTimeout(() => { |
| 58 | + |
| 59 | + try { |
| 60 | + expect(component.find('.name p').at(0).text()).toEqual('Visit Date: 10/01/2017'); |
| 61 | + expect(component.find('.name p').at(1).text()).toEqual('Address: 7 Street'); |
| 62 | + expect(component.find('.name p').at(2).text()).toEqual('Area: Center'); |
| 63 | + expect(component.find('.followers p').at(0).text()).toEqual('R$ 300'); |
| 64 | + expect(component.find('.followers p').at(1).text()).toEqual('Value Received'); |
| 65 | + expect(component.find('.likes p').at(0).text()).toEqual('150'); |
| 66 | + expect(component.find('.likes p').at(1).text()).toEqual('Sales Quantity'); |
| 67 | + done(); |
| 68 | + } catch(e) { |
| 69 | + console.log(e); |
| 70 | + } |
| 71 | + }, 0); |
| 72 | + }); |
| 73 | + |
| 74 | + it('Visit should show error message', (done) => { |
| 75 | + |
| 76 | + let id = '123abc'; |
| 77 | + let response = { error:"Visit Not Found" }; |
| 78 | + let Visit; |
| 79 | + let component; |
| 80 | + let mockAdapter = new MockAdapter(axios); |
| 81 | + |
| 82 | + mockAdapter.onGet(HOST + '/api/v1/visit/' + id).reply(404, response); |
| 83 | + |
| 84 | + Visit = require('components/Visit/Show/Visit').default; |
| 85 | + |
| 86 | + component = shallow( |
| 87 | + <Visit params={ { id: id} }/> |
| 88 | + ); |
| 89 | + |
| 90 | + setTimeout(() => { |
| 91 | + |
| 92 | + try { |
| 93 | + component.update(); |
| 94 | + expect(component.render().text()).toEqual('Visit Not Found'); |
| 95 | + done(); |
| 96 | + } catch(e) { |
| 97 | + console.log(e); |
| 98 | + } |
| 99 | + }, 0); |
| 100 | + }); |
| 101 | + |
| 102 | + it('Visit should show default error message', (done) => { |
| 103 | + |
| 104 | + let id = '123abc'; |
| 105 | + let response = {}; |
| 106 | + let Visit; |
| 107 | + let component; |
| 108 | + let mockAdapter = new MockAdapter(axios); |
| 109 | + |
| 110 | + mockAdapter.onGet(HOST + '/api/v1/visit/' + id).reply(503, response); |
| 111 | + |
| 112 | + Visit = require('components/Visit/Show/Visit').default; |
| 113 | + |
| 114 | + component = shallow( |
| 115 | + <Visit params={ { id: id} }/> |
| 116 | + ); |
| 117 | + |
| 118 | + setTimeout(() => { |
| 119 | + try { |
| 120 | + component.update(); |
| 121 | + expect(component.render().text()).toEqual('Error Found: Trying get visit'); |
| 122 | + done(); |
| 123 | + } catch(e) { |
| 124 | + console.log(e); |
| 125 | + } |
| 126 | + }, 0); |
| 127 | + }); |
| 128 | + |
| 129 | +}); |
| 130 | + |
| 131 | + |
0 commit comments