Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e60414f

Browse files
committedOct 12, 2017
Unit test.
1 parent 09b50c4 commit e60414f

File tree

6 files changed

+69
-101
lines changed

6 files changed

+69
-101
lines changed
 

‎test/unit/assertions/nodeAssertion.js

Lines changed: 0 additions & 11 deletions
This file was deleted.

‎test/unit/specs/plugin/behaviour-for-alert.spec.js

Lines changed: 22 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,69 +2,45 @@
22
* Created by Emmy on 10/8/2017.
33
*/
44

5-
import * as HOOKS from '../../utilities/hooks'
6-
import {getElem} from '../../utilities/helpers'
7-
import { assert, expect } from 'chai'
85
import Vue from 'vue'
96
import Promise from 'promise-polyfill'
7+
import {assert, expect} from 'chai'
8+
import * as HOOKS from '../../utilities/hooks'
9+
import {nodeLength} from '../../utilities/helpers'
1010

1111
Vue.config.productionTip = false
1212

1313
describe('Calling "alert()"', function () {
14-
// this.timeout(3000);
15-
beforeEach(HOOKS.sanitizeAndPrepareWindow)
14+
let dg
15+
16+
before(HOOKS.sanitizeAndPrepareWindow)
17+
before(function (done) {
18+
dg = window.vm.triggerAlert()
19+
Vue.nextTick(done) // be sure done has updated before proceeding
20+
})
1621

1722
it('Should return a promise', function () {
18-
let dg = window.vm.triggerAlert()
1923
expect(dg).to.be.instanceOf(Promise)
2024
})
2125

22-
it('Should make the dialog visible', function (done) {
23-
let elem = getElem('dg-container')
24-
window.vm.triggerAlert().then(() => {}).catch(() => {})
25-
26-
Vue.nextTick(() => {
27-
try{
28-
assert.equal(getElem('dg-container').length, 1)
29-
done()
30-
}catch(err){
31-
done(new Error(err.toString()))
32-
}
33-
})
26+
it('Should make the dialog visible', function () {
27+
assert.strictEqual(nodeLength('.dg-container'), 1)
3428
})
35-
})
36-
37-
describe('with #alert(), Clicking "ok"', function () {
38-
// this.timeout(3000);
39-
beforeEach(HOOKS.sanitizeAndPrepareWindow)
4029

41-
it('Should resolve the promise', function (done) {
42-
window.vm.triggerAlert()
43-
.then(() => {done()}) // expected
44-
.catch(err => { done(err.toString())})
30+
it('Should make the ok button visible', function () {
31+
assert.strictEqual(nodeLength('.dg-btn--ok'), 1)
32+
})
4533

46-
Vue.nextTick(() => {
47-
window.vm.clickDialogBtn('ok')
48-
})
34+
it('Should make the cancel button visible', function () {
35+
assert.strictEqual(nodeLength('.dg-btn--cancel'), 0)
4936
})
5037
})
5138

52-
describe('with #alert(), user ', function () {
53-
// this.timeout(3000);
39+
describe('Calling "alert()", then Click "ok"', function () {
5440
before(HOOKS.sanitizeAndPrepareWindow)
55-
before(function(){
56-
window.vm.triggerAlert()
57-
})
5841

59-
it('Should see ok button', function () {
60-
Vue.nextTick(() => {
61-
assert.equal(getElem('dg-btn--ok').length, 1)
62-
})
63-
})
64-
65-
it('Should not see cancel button', function () {
66-
Vue.nextTick(() => {
67-
assert.equal(getElem('dg-btn--cancel').length, 0)
68-
})
42+
it('Should resolve the promise', function (done) {
43+
window.vm.triggerAlert().then(() => {done()}) // expected
44+
Vue.nextTick(() => window.vm.clickDialogBtn('ok'))
6945
})
70-
})
46+
})

‎test/unit/specs/plugin/behaviour-for-confirm.spec.js

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,54 +3,55 @@
33
*/
44

55
import * as HOOKS from '../../utilities/hooks'
6-
import {getElem} from '../../utilities/helpers'
6+
import {getElem, nodeLength} from '../../utilities/helpers'
77
import { assert, expect } from 'chai'
88
import Vue from 'vue'
99
import Promise from 'promise-polyfill'
1010

1111
Vue.config.productionTip = false
1212

1313
describe('Calling "confirm()"', function () {
14-
// this.timeout(3000);
15-
beforeEach(HOOKS.sanitizeAndPrepareWindow)
14+
let dg
15+
16+
before(HOOKS.sanitizeAndPrepareWindow)
17+
before(function (done) {
18+
dg = window.vm.triggerConfirm()
19+
Vue.nextTick(done) // be sure done has updated before proceeding
20+
})
1621

1722
it('Should return a promise', function () {
18-
let dg = window.vm.triggerConfirm()
1923
expect(dg).to.be.instanceOf(Promise)
2024
})
2125

22-
it('Should make the dialog visible', function (done) {
23-
window.vm.triggerConfirm()
24-
let nodes = getElem('dg-container')
25-
26-
Vue.nextTick(() => {
27-
try{
28-
assert.equal(nodes.length, 1)
29-
done()
30-
}catch(err){
31-
done(new Error(err.toString()))
32-
}
33-
})
26+
it('Should make the dialog visible', function () {
27+
assert.strictEqual(nodeLength('.dg-container'), 1)
3428
})
35-
})
3629

30+
it('Should make the ok button visible', function () {
31+
assert.strictEqual(nodeLength('.dg-btn--ok'), 1)
32+
})
3733

38-
describe('with #confirm(), user ', function () {
39-
this.timeout(100);
40-
before(HOOKS.sanitizeAndPrepareWindow)
41-
before(function () {
42-
window.vm.triggerConfirm()
34+
it('Should make the cancel button visible', function () {
35+
assert.strictEqual(nodeLength('.dg-btn--cancel'), 1)
4336
})
37+
})
38+
39+
describe('Clicking \'ok\' on #confirm()', function () {
40+
beforeEach(HOOKS.sanitizeAndPrepareWindow)
4441

45-
it('Should see ok button', function () {
46-
Vue.nextTick(() => {
47-
assert.equal(getElem('dg-btn--ok').length, 1)
48-
})
42+
it('Should resolve the promise', function (done) {
43+
window.vm.triggerConfirm().then(() => {done()}) // expected
44+
Vue.nextTick(() => window.vm.clickDialogBtn('ok'))
4945
})
46+
})
47+
48+
describe('Clicking \'cancel\' on #confirm()', function () {
49+
beforeEach(HOOKS.sanitizeAndPrepareWindow)
5050

51-
it('Should see cancel button', function () {
52-
Vue.nextTick(() => {
53-
assert.equal(getElem('dg-btn--cancel').length, 1)
54-
})
51+
it('Should reject the promise', function (done) {
52+
window.vm.triggerConfirm()
53+
.then(() => {done(new Error('Cancel button should Reject promise'))})
54+
.catch(() => {done()}) // expected
55+
Vue.nextTick(() => window.vm.clickDialogBtn('cancel'))
5556
})
5657
})

‎test/unit/utilities/helpers.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
* Created by Emmy on 10/11/2017.
33
*/
44

5-
export function getElem(selector) {
6-
return document.getElementsByClassName(selector)
7-
// return document.querySelector(selector)
8-
}
5+
export function getElem(selector, all = false) {
6+
return all ? document.querySelectorAll(selector) : document.querySelector(selector)
7+
}
8+
export function nodeLength(selector) {
9+
return getElem(selector, true).length
10+
}

‎test/unit/utilities/hooks.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import {setupVmWithLocalVue} from './initializers'
77
import Promise from 'promise-polyfill'
88

99
export function sanitizeAndPrepareWindow(done) {
10-
this.timeout(500)
11-
1210
Promise.resolve()
1311
.then((function (){
1412
return new Promise(function (resolve) {
@@ -29,14 +27,15 @@ export function sanitizeAndPrepareWindow(done) {
2927
elem.remove()
3028
delete window.vm
3129
}
32-
Vue.nextTick(resolve)
30+
resolve()
3331
})
3432
})).then(() => {
3533
// set them up again
3634
window.vm = setupVmWithLocalVue()
37-
done()
35+
// Proceed
36+
Vue.nextTick(done)
3837
}).catch((err) => {
39-
done(new Error('prepareWindow: '+ err.toString()))
38+
done(new Error(err.toString()))
4039
})
4140

4241
}

‎test/unit/utilities/initializers.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ export function setupVmWithLocalVue() {
2929
})).$mount(node)
3030
}
3131

32-
function clickDialogBtn(type = 'ok', idx = null) {
33-
let node, nodes = (type === 'ok') ? getElem('dg-btn--ok') : getElem('dg-btn--cancel')
32+
function clickDialogBtn(dgBtn = 'ok', idx = null) {
33+
let selector = (dgBtn === 'ok') ? '.dg-btn--ok' : '.dg-btn--cancel'
34+
let node, nodes = getElem(selector, true)
3435

3536
if (nodes.length > 0) {
3637
if (idx === null) {
@@ -41,9 +42,9 @@ function clickDialogBtn(type = 'ok', idx = null) {
4142
for (let i = 0; i < nodes.length; i++) {
4243
clickNode(nodes[i])
4344
}
44-
} else {
45+
} else if (node = nodes[parseInt(idx)]) {
4546
// click at index
46-
(node = nodes[parseInt(idx)]) && clickNode(node)
47+
clickNode(node)
4748
}
4849
}
49-
}
50+
}

0 commit comments

Comments
 (0)
Please sign in to comment.