|
| 1 | +import Vue from 'vue'; |
| 2 | +import VueRest from '../../../index.js'; |
| 3 | +import moxios from 'moxios'; |
| 4 | + |
| 5 | +import ApiForm from '../../../mixins/ApiForm'; |
| 6 | + |
| 7 | +describe('Testing ApiForm mixin', () => { |
| 8 | + |
| 9 | + Vue.use(VueRest); |
| 10 | + Vue.mixin(ApiForm); |
| 11 | + |
| 12 | + beforeEach(() => { |
| 13 | + moxios.install(Vue.api); |
| 14 | + }); |
| 15 | + |
| 16 | + afterEach(() => { |
| 17 | + moxios.uninstall(Vue.api); |
| 18 | + }); |
| 19 | + |
| 20 | + const newFood = { |
| 21 | + food: 'Sushi', |
| 22 | + flavor: 'Tuna hosomaki', |
| 23 | + }; |
| 24 | + |
| 25 | + const comp = new Vue({ |
| 26 | + render: () => {}, |
| 27 | + propsData: { |
| 28 | + route: '/foods', |
| 29 | + obj: newFood, |
| 30 | + }, |
| 31 | + }).$mount(); |
| 32 | + |
| 33 | + test('Should set requestObj as obj prop', () => { |
| 34 | + expect(comp.requestObj).toBe(newFood); |
| 35 | + }); |
| 36 | + |
| 37 | + test('Should register a new object', (done) => { |
| 38 | + comp.save((response) => comp.requestObj = response.data); |
| 39 | + |
| 40 | + moxios.wait(() => { |
| 41 | + const request = moxios.requests.mostRecent(); |
| 42 | + const responseObj = { ...newFood, id: 1 }; |
| 43 | + |
| 44 | + request.respondWith({ |
| 45 | + status: 201, |
| 46 | + response: { |
| 47 | + responseObj, |
| 48 | + }, |
| 49 | + }).then((response) => { |
| 50 | + expect(comp.requestObj).toBe(response.data); |
| 51 | + done(); |
| 52 | + }); |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + test('Should call errCallback on save new obj', (done) => { |
| 57 | + const errorMsg = 'Error: Request failed with status code 403'; |
| 58 | + let error = ''; |
| 59 | + |
| 60 | + comp.save((response) => {}, (err) => error = err.toString()); |
| 61 | + |
| 62 | + moxios.wait(() => { |
| 63 | + const request = moxios.requests.mostRecent(); |
| 64 | + |
| 65 | + request.respondWith({ |
| 66 | + status: 403, |
| 67 | + }).then((response) => { |
| 68 | + expect(error).toBe(errorMsg); |
| 69 | + done(); |
| 70 | + }); |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + |
| 75 | + test('Should update an object', (done) => { |
| 76 | + Object.assign(comp.requestObj, { id: 1 }); |
| 77 | + comp.save((response) => comp.requestObj = response.data); |
| 78 | + |
| 79 | + moxios.wait(() => { |
| 80 | + const request = moxios.requests.mostRecent(); |
| 81 | + const responseObj = { food: 'Sushi', flavor: 'Salmon nigiri', id: 1 }; |
| 82 | + |
| 83 | + request.respondWith({ |
| 84 | + status: 200, |
| 85 | + response: { |
| 86 | + responseObj, |
| 87 | + }, |
| 88 | + }).then((response) => { |
| 89 | + expect(comp.requestObj).toBe(response.data); |
| 90 | + done(); |
| 91 | + }); |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + test('Should call errCallback on save new obj', (done) => { |
| 96 | + Object.assign(comp.requestObj, { id: 1 }); |
| 97 | + const errorMsg = 'Error: Request failed with status code 403'; |
| 98 | + let error = ''; |
| 99 | + |
| 100 | + comp.save((response) => {}, (err) => error = err.toString()); |
| 101 | + |
| 102 | + moxios.wait(() => { |
| 103 | + const request = moxios.requests.mostRecent(); |
| 104 | + |
| 105 | + request.respondWith({ |
| 106 | + status: 403, |
| 107 | + }).then((response) => { |
| 108 | + expect(error).toBe(errorMsg); |
| 109 | + done(); |
| 110 | + }); |
| 111 | + }); |
| 112 | + }); |
| 113 | + |
| 114 | + |
| 115 | + let comp2Err = ''; |
| 116 | + const comp2 = new Vue({ |
| 117 | + render: () => {}, |
| 118 | + propsData: { |
| 119 | + route: '/foods', |
| 120 | + objId: 1, |
| 121 | + loadErrCb: (err) => comp2Err = err.toString(), |
| 122 | + }, |
| 123 | + }); |
| 124 | + |
| 125 | + test('Should load succesfully an obj', (done) => { |
| 126 | + comp2.$mount(); |
| 127 | + |
| 128 | + moxios.wait(() => { |
| 129 | + const request = moxios.requests.mostRecent(); |
| 130 | + const responseObj = { food: 'Sushi', flavor: 'Salmon skin roll' , id: 1 }; |
| 131 | + |
| 132 | + request.respondWith({ |
| 133 | + status: 200, |
| 134 | + response: { |
| 135 | + responseObj, |
| 136 | + }, |
| 137 | + }).then((response) => { |
| 138 | + expect(comp2.requestObj).toBe(response.data); |
| 139 | + done(); |
| 140 | + }); |
| 141 | + }); |
| 142 | + }); |
| 143 | + |
| 144 | + test('Should load succesfully an obj', (done) => { |
| 145 | + comp2.$mount(); |
| 146 | + |
| 147 | + moxios.wait(() => { |
| 148 | + const request = moxios.requests.mostRecent(); |
| 149 | + const errorMsg = 'Error: Request failed with status code 403'; |
| 150 | + |
| 151 | + request.respondWith({ |
| 152 | + status: 403, |
| 153 | + }).then((response) => { |
| 154 | + expect(comp2Err).toBe(errorMsg); |
| 155 | + done(); |
| 156 | + }); |
| 157 | + }); |
| 158 | + }); |
| 159 | +}); |
| 160 | + |
0 commit comments