|
| 1 | +jest.enableAutomock(); |
| 2 | +jest.dontMock('components/Client/Profile/Client'); |
| 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/Client'); |
| 9 | + |
| 10 | +describe('Test Client', () => { |
| 11 | + require('../tests/__mocks__/LocalStorageMock'); |
| 12 | + |
| 13 | + const React = require('react'); |
| 14 | + const Enzyme = require('enzyme'); |
| 15 | + const shallow = Enzyme.shallow; |
| 16 | + |
| 17 | + let axios = require('axios'); |
| 18 | + let MockAdapter = require('axios-mock-adapter'); |
| 19 | + |
| 20 | + it('Client should show mocked data', (done) => { |
| 21 | + |
| 22 | + let id = '123abc'; |
| 23 | + let response = { |
| 24 | + client: [ |
| 25 | + { |
| 26 | + id: id, |
| 27 | + name: 'Jon Snow', |
| 28 | + address: '7 Street', |
| 29 | + city: 'Winterfell', |
| 30 | + area: { |
| 31 | + _id: 'Center', |
| 32 | + parents: 'Center' |
| 33 | + } |
| 34 | + }, |
| 35 | + ] |
| 36 | + }; |
| 37 | + let Client; |
| 38 | + let component; |
| 39 | + let mockAdapter = new MockAdapter(axios); |
| 40 | + |
| 41 | + mockAdapter.onGet('http://localhost:3000/api/v1/client/' + id).reply(200, response); |
| 42 | + |
| 43 | + Client = require('components/Client/Profile/Client').default; |
| 44 | + |
| 45 | + component = shallow( |
| 46 | + <Client params={ { id: id} }/> |
| 47 | + ); |
| 48 | + |
| 49 | + setTimeout(() => { |
| 50 | + |
| 51 | + expect(component.find('.name p').at(0).text()).toEqual('Jon Snow'); |
| 52 | + expect(component.find('.name p').at(1).text()).toEqual('7 Street'); |
| 53 | + expect(component.find('.name p').at(2).text()).toEqual('Winterfell'); |
| 54 | + expect(component.find('.followers p').at(0).text()).toEqual('Area'); |
| 55 | + expect(component.find('.followers p').at(1).text()).toEqual('Center'); |
| 56 | + expect(component.find('.nav-menu span a').at(0).text()).toEqual('Visited'); |
| 57 | + expect(component.find('.nav-menu span a').at(1).text()).toEqual('Update'); |
| 58 | + expect(component.find('.nav-menu span a').at(2).text()).toEqual('Schedule'); |
| 59 | + expect(component.find('.nav-menu span a').at(3).text()).toEqual('Delete'); |
| 60 | + done(); |
| 61 | + }, 0); |
| 62 | + }); |
| 63 | + |
| 64 | + it('Client should show error message', (done) => { |
| 65 | + |
| 66 | + let id = '123abc'; |
| 67 | + let response = { error:"Client Not Found" }; |
| 68 | + let promises = []; |
| 69 | + let Client; |
| 70 | + let component; |
| 71 | + let mockAdapter = new MockAdapter(axios); |
| 72 | + |
| 73 | + mockAdapter.onGet('http://localhost:3000/api/v1/client/' + id).reply(404, response); |
| 74 | + |
| 75 | + Client = require('components/Client/Profile/Client').default; |
| 76 | + |
| 77 | + component = shallow( |
| 78 | + <Client params={ { id: id} }/> |
| 79 | + ); |
| 80 | + |
| 81 | + setTimeout(() => { |
| 82 | + |
| 83 | + component.update(); |
| 84 | + expect(component.render().text()).toEqual('Client Not Found'); |
| 85 | + done(); |
| 86 | + }, 0); |
| 87 | + }); |
| 88 | + |
| 89 | + it('Client should show default error message', (done) => { |
| 90 | + |
| 91 | + let id = '123abc'; |
| 92 | + let response = {}; |
| 93 | + let promises = []; |
| 94 | + let Client; |
| 95 | + let component; |
| 96 | + let mockAdapter = new MockAdapter(axios); |
| 97 | + |
| 98 | + mockAdapter.onGet('http://localhost:3000/api/v1/client/' + id).reply(503, response); |
| 99 | + |
| 100 | + Client = require('components/Client/Profile/Client').default; |
| 101 | + |
| 102 | + component = shallow( |
| 103 | + <Client params={ { id: id} }/> |
| 104 | + ); |
| 105 | + |
| 106 | + setTimeout(() => { |
| 107 | + |
| 108 | + component.update(); |
| 109 | + expect(component.render().text()).toEqual('Error Found: Trying get client'); |
| 110 | + done(); |
| 111 | + }, 0); |
| 112 | + }); |
| 113 | + |
| 114 | +}); |
| 115 | + |
| 116 | + |
0 commit comments