Skip to content

Commit 67546af

Browse files
committed
cleaning form before submission
1 parent ab48beb commit 67546af

File tree

3 files changed

+35
-11
lines changed

3 files changed

+35
-11
lines changed

src/main.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import VueRouter from 'vue-router'
55
import app from './app'
66
import grid from './components/grid'
77
import con_modal from './components/con-modal'
8-
import {to_markdown} from './utils'
8+
import {to_markdown, clean} from './utils'
99

1010
let dsn = process.env.NODE_ENV === 'production' && 'https://[email protected]/128441'
1111
Raven.config(dsn, {release: process.env.RELEASE}).addPlugin(RavenVue, Vue).install()
@@ -177,7 +177,7 @@ response text: "${xhr.responseText}"`)
177177
}
178178
},
179179
submit_enquiry: function (callback) {
180-
let data = JSON.stringify(this.enquiry_data)
180+
let data = JSON.stringify(clean(this.enquiry_data))
181181
let xhr = new window.XMLHttpRequest()
182182
let url = `${config.api_root}/${public_key}/enquiry`
183183
xhr.open('POST', url)

src/utils.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,17 @@ function to_markdown (t) {
1313
return marked(t)
1414
}
1515
}
16-
export { to_markdown }
16+
17+
const clean = (obj) => {
18+
let new_obj = {}
19+
for (let [k, v] of Object.entries(obj)) {
20+
if (typeof v === 'object') {
21+
new_obj[k] = clean(v)
22+
} else if (v !== null && v !== undefined && v !== '') {
23+
new_obj[k] = v
24+
}
25+
}
26+
return new_obj
27+
}
28+
29+
export { to_markdown, clean }

test/unit/specs/main.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -165,27 +165,38 @@ describe('main.js', () => {
165165
server.respondWith('/public-key/contractors', dft_response)
166166
server.respondWith('/public-key/enquiry',
167167
[200, {'Content-Type': 'application/json'}, JSON.stringify(enquiry_options)])
168-
server.respondWith('POST', '/public-key/enquiry',
169-
[201, {'Content-Type': 'application/json'}, '{"response": "ok"}'])
168+
server.respondWith('POST', '/public-key/enquiry', (xhr, id) => {
169+
let obj = JSON.parse(xhr.requestBody)
170+
expect(obj).to.deep.equal({first_field: 'foobar', attributes: {foo: 'X'}})
171+
xhr.respond(201, {'Content-Type': 'application/json'}, '{"response": "ok"}')
172+
})
170173
})
171174
after(() => { server.restore() })
172175

173-
it('should post enquiry data', done => {
176+
it('should post enquiry data', async () => {
174177
let el = document.createElement('div')
175178
el.setAttribute('id', 'socket')
176179
document.body.appendChild(el)
177180

178181
const vm = socket('public-key')
179182
vm.get_enquiry()
180183

181-
setTimeout(() => {
182-
vm.$set(vm.enquiry_data, 'first_field', 'foobar')
183-
expect(vm.enquiry_data).to.deep.equal({first_field: 'foobar'})
184+
await sleep(50)
185+
vm.enquiry_data = {
186+
first_field: 'foobar',
187+
another_field: '',
188+
attributes: {
189+
foo: 'X',
190+
bar: '',
191+
}
192+
}
193+
expect(vm.enquiry_data).to.not.deep.equal({})
194+
await new Promise((resolve, reject) => {
184195
let callback = () => {
185196
expect(vm.enquiry_data).to.deep.equal({})
186-
done()
197+
resolve()
187198
}
188199
vm.submit_enquiry(callback)
189-
}, 50)
200+
})
190201
})
191202
})

0 commit comments

Comments
 (0)