Skip to content

Commit cf8e452

Browse files
committed
Finished Form class tests
1 parent a8b3946 commit cf8e452

File tree

3 files changed

+42
-24
lines changed

3 files changed

+42
-24
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"main": "dist/Form.js",
66
"scripts": {
77
"test": "node ./node_modules/jest/bin/jest",
8-
"build": "node ./node_modules/babel-cli/bin/babel src --out-dir dist"
8+
"build": "node ./node_modules/babel-cli/bin/babel src --out-dir dist",
9+
"preversion": "npm test"
910
},
1011
"repository": {
1112
"type": "git",

src/Form.js

+13-9
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
let FormErrors = require('./FormErrors');
22
let RequestDataContainer = require('./RequestDataContainer');
33

4-
if (window.axios == 'undefined') {
5-
let axios = require('axios');
6-
}
7-
84
/**
95
* this class handles everything that concerns
106
* about forms. Submit and errors and considered too
@@ -25,6 +21,12 @@ class Form {
2521
this.method = method;
2622
this.clearAfterResponse = clearAfterResponse;
2723
this.errors = new FormErrors();
24+
25+
if (window.axios == undefined) {
26+
this.axios = require('axios');
27+
} else {
28+
this.axios = window.axios;
29+
}
2830
}
2931

3032
static makeFrom(data, method = 'post', clearAfterResponse = false) {
@@ -54,10 +56,6 @@ class Form {
5456
container.set(field, this[field]);
5557
}
5658

57-
if (this.method != 'post') {
58-
container.set('_method', this.method);
59-
}
60-
6159
return container.data;
6260
}
6361

@@ -89,8 +87,14 @@ class Form {
8987
* @return {Promise}
9088
*/
9189
submit(url, dataType = 'form_data') {
90+
let supportedMethods = ['post', 'put', 'patch'];
91+
92+
if (supportedMethods.indexOf(this.method.toLowerCase()) == -1) {
93+
throw new Error('Unssuported method');
94+
}
95+
9296
return new Promise((resolve, reject) => {
93-
axios.post(url, this.data(dataType))
97+
this.axios[this.method](url, this.data(dataType))
9498
.then(response => {
9599
this.formSubmitSucceded(response);
96100
resolve(response);

tests/form.test.js

+27-14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
let Form = require('../src/Form');
22
let moxios = require('moxios');
33

4-
54
test('can create a form with data, method (optional) and clearAfterResponse (optional)', () => {
65
let form = Form.makeFrom({name: 'test'}, 'post', false);
76

@@ -62,21 +61,35 @@ test('it retrieves the form data as a FormData', () => {
6261
});
6362

6463
test('can submit the form and use promises to receive the results', () => {
65-
moxios.install();
66-
let form = Form.makeFrom({name: 'test', email: 'test@mail.com'});
64+
let form = Form.makeFrom({name: 'test', email: 'test@mail.com'}, 'post');
65+
moxios.install(form.axios);
6766

68-
form.submit('theurl')
69-
.then(response => {
70-
console.log(response);
71-
expect(response.data).toEqual('correct');
67+
moxios.wait(function () {
68+
moxios.requests.mostRecent().respondWith({
69+
status: 200,
70+
response: 'done'
7271
});
72+
});
73+
74+
return form.submit('url').then(response => {
75+
expect(response.data).toBe("done");
76+
moxios.uninstall(form.axios);
77+
});
78+
});
79+
80+
test('can submit the form and use promises to receive the validation errors', () => {
81+
let form = Form.makeFrom({name: 'test', email: 'test@mail.com'}, 'post');
82+
moxios.install(form.axios);
7383

7484
moxios.wait(function () {
75-
let request = moxios.requests.mostRecent()
76-
request.respondWith({
77-
status: 200,
78-
data: "correct"
79-
})
80-
})
81-
moxios.uninstall();
85+
moxios.requests.mostRecent().respondWith({
86+
status: 422,
87+
response: { errors: { name: ['failed'] } }
88+
});
89+
});
90+
91+
return form.submit('url').catch(error => {
92+
expect(form.errorsFor('name')).toBe("failed");
93+
moxios.uninstall(form.axios);
94+
});
8295
});

0 commit comments

Comments
 (0)