Skip to content

Commit 330b839

Browse files
authored
Merge pull request #5 from kimuraz/test_form
Test form
2 parents 715a7f5 + a5bb5ad commit 330b839

File tree

5 files changed

+208
-264
lines changed

5 files changed

+208
-264
lines changed

mixins/ApiForm.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,23 @@ export default {
1414
required: false,
1515
default: () => null,
1616
},
17+
loadErrCb: {
18+
type: Function,
19+
required: false,
20+
defaut: (err) => {},
21+
},
1722
},
1823
data() {
1924
return {
2025
requestObj: {},
2126
};
2227
},
23-
created() {
28+
mounted() {
2429
if (this.obj) {
2530
this.requestObj = this.obj;
2631
}
2732
if (this.objId) {
28-
this.load(this.objId);
33+
this.load(this.objId, this.loadErrCb);
2934
}
3035
},
3136
methods: {
@@ -37,7 +42,7 @@ export default {
3742
});
3843
},
3944
update(saveCallback = (response) => {}, errCallback = (err) => {}) {
40-
this.$api.patch(this.route, this.requestObj).then((response) => {
45+
this.$api.patch(`${this.route}/${this.requestObj.id}/`, this.requestObj).then((response) => {
4146
saveCallback(response);
4247
}).catch((err) => {
4348
errCallback(err);

package.json

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,13 @@
3838
"eslint-plugin-html": "^3.0.0",
3939
"eslint-plugin-import": "^2.2.0",
4040
"eventsource-polyfill": "^0.9.6",
41-
"express": "^4.14.1",
4241
"extract-text-webpack-plugin": "^2.0.0",
4342
"file-loader": "^0.11.1",
4443
"friendly-errors-webpack-plugin": "^1.1.3",
4544
"html-webpack-plugin": "^2.28.0",
4645
"http-proxy-middleware": "^0.17.3",
4746
"inject-loader": "^3.0.0",
4847
"jest": "^22.0.6",
49-
"karma-coverage": "^1.1.1",
50-
"karma-mocha": "^1.3.0",
51-
"karma-phantomjs-launcher": "^1.0.2",
52-
"karma-phantomjs-shim": "^1.4.0",
53-
"karma-sinon-chai": "^1.3.1",
54-
"karma-sourcemap-loader": "^0.3.7",
55-
"karma-spec-reporter": "0.0.31",
56-
"karma-webpack": "^2.0.2",
5748
"moxios": "^0.4.0",
5849
"opn": "^5.1.0",
5950
"optimize-css-assets-webpack-plugin": "^2.0.0",
@@ -67,7 +58,6 @@
6758
"uglifyjs-webpack-plugin": "^0.4.6",
6859
"url-loader": "^0.5.8",
6960
"vue-loader": "^13.0.4",
70-
"vue-style-loader": "^3.0.1",
7161
"vue-template-compiler": "^2.4.2",
7262
"webpack": "^3.8.1",
7363
"webpack-bundle-analyzer": "^2.2.1",
@@ -87,7 +77,7 @@
8777
"jest": {
8878
"moduleNameMapper": {
8979
"mixins/(.*)$": "<rootDir>/mixins/$1.js",
90-
"^vue$": "vue/dist/vue.common.js"
80+
"^vue$": "vue/dist/vue.min.js"
9181
},
9282
"coverageDirectory": "./coverage/",
9383
"collectCoverage": true

test/unit/mixins/ApiForm.spec.js

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
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+

test/unit/mixins/Install.spec.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import Vue from 'vue';
2+
import VueRest from '../../../index.js';
3+
4+
describe('Testing install function', () => {
5+
6+
Vue.use(VueRest, {
7+
axiosOptions: {
8+
baseURL: 'http://localhost:8000',
9+
},
10+
});
11+
12+
test('Should apply axios settings when axiosOptions is set', () => {
13+
expect(Vue.api.defaults.baseURL).toBe('http://localhost:8000');
14+
});
15+
});

0 commit comments

Comments
 (0)