Skip to content

Commit 725a3da

Browse files
committed
[Alert] Default type change to info.
1 parent 8f01e56 commit 725a3da

File tree

7 files changed

+107
-94
lines changed

7 files changed

+107
-94
lines changed

docs/pages/components/Alert.md

Lines changed: 51 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,21 @@ Wrap any text or HTML in `<alert>` and use one of the four alert types (`success
1515
<!-- Live demo -->
1616
```
1717

18-
## Dismissible Alerts
19-
20-
* Use `dismissible` to allow user to dismiss alerts.
21-
* Use `duration` to auto dismiss alerts after milliseconds.
22-
* These 2 props can be used in combinations.
18+
## Dismissible
2319

20+
Use `dismissible` to allow user to dismiss alerts.
2421

2522
```html
2623
<template>
2724
<section>
28-
<alert type="warning" v-if="show1" :dismissible="true" @dismissed="show1 = false" id="alert-test"><b>Warning!</b> Better check yourself, you're not looking too good.</alert>
29-
<alert v-for="(item, index) in alerts"
30-
:dismissible="item.dismissible"
31-
:type="item.type"
32-
:duration="item.duration"
33-
:key="item.key"
34-
@dismissed="alerts.splice(index, 1)">
35-
<span v-if="item.duration">This alert <b>will dismiss after {{item.duration}}ms</b>.</span>
36-
<span v-else>This alert <b>will not dismiss over time</b>.</span>
25+
<alert type="warning" v-if="show1" :dismissible="true" @dismissed="show1 = false" id="alert-test">
26+
<b>Warning!</b> Better check yourself, you're not looking too good.
27+
</alert>
28+
<alert v-for="(item, index) in alerts" :dismissible="true" :key="item.key" @dismissed="alerts.splice(index, 1)">
29+
<b>Heads up!</b> This alert needs your attention, but it's not super important.
3730
</alert>
3831
<hr/>
39-
<button type="button" class="btn btn-default" id="add-alert-1" @click="addAlert(false)">Add Alert (Dismiss After 2000ms)</button>
40-
<button type="button" class="btn btn-default" id="add-alert-2" @click="addAlert(true)">Add Alert (Dismissible)</button>
32+
<button type="button" class="btn btn-primary" id="add-alert-1" @click="addDismissibleAlert()">Add Dismissible Alert</button>
4133
</section>
4234
</template>
4335

@@ -46,18 +38,46 @@ Wrap any text or HTML in `<alert>` and use one of the four alert types (`success
4638
data () {
4739
return {
4840
show1: true,
49-
alerts: [],
41+
alerts: []
42+
}
43+
},
44+
methods: {
45+
addDismissibleAlert () {
46+
this.alerts.push({key: new Date().getTime()})
47+
}
48+
}
49+
}
50+
</script>
51+
52+
<!-- Live demo -->
53+
```
54+
55+
## Auto Dismissing
56+
57+
Use `duration` in milliseconds to auto dismiss alert.
58+
59+
```html
60+
<template>
61+
<section>
62+
<alert v-for="(item, index) in alerts2" :duration="duration" :key="item.key" @dismissed="alerts2.splice(index, 1)">
63+
This alert <b>will dismiss after {{duration}}ms</b>.
64+
</alert>
65+
<hr/>
66+
<button type="button" class="btn btn-primary" id="add-alert-2" @click="addAutoDismissAlert()">Add Auto Dismiss Alert</button>
67+
</section>
68+
</template>
69+
70+
<script>
71+
export default {
72+
data () {
73+
return {
74+
alerts2: [],
5075
duration: 2000
5176
}
5277
},
5378
methods: {
54-
addAlert (dismissible) {
55-
this.alerts.push({
56-
type: 'success',
57-
dismissible: dismissible,
58-
duration: dismissible ? 0 : this.duration,
59-
key: new Date().getTime()
60-
})
79+
addAutoDismissAlert () {
80+
this.alerts2.push({key: new Date().getTime()})
6181
}
6282
}
6383
}
@@ -119,7 +139,7 @@ Wrap any text or HTML in `<alert>` and use one of the four alert types (`success
119139
<tr>
120140
<td nowrap="nowrap"><code>type</code></td>
121141
<td>String</td>
122-
<td>success</td>
142+
<td>info</td>
123143
<td></td>
124144
<td>Alert type (success, info, warning, danger).</td>
125145
</tr>
@@ -172,19 +192,18 @@ Wrap any text or HTML in `<alert>` and use one of the four alert types (`success
172192
data () {
173193
return {
174194
alerts: [],
195+
alerts2: [],
175196
show1: true,
176197
show2: true,
177198
duration: 2000
178199
}
179200
},
180201
methods: {
181-
addAlert (dismissible) {
182-
this.alerts.push({
183-
type: 'success',
184-
dismissible: dismissible,
185-
duration: dismissible ? 0 : this.duration,
186-
key: new Date().getTime()
187-
})
202+
addDismissibleAlert () {
203+
this.alerts.push({key: new Date().getTime()})
204+
},
205+
addAutoDismissAlert () {
206+
this.alerts2.push({key: new Date().getTime()})
188207
}
189208
}
190209
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
"html-webpack-plugin": "^2.28.0",
9595
"http-proxy-middleware": "^0.17.3",
9696
"inject-loader": "^2.0.1",
97+
"jquery": "^3.2.1",
9798
"karma": "^1.4.1",
9899
"karma-coverage": "^1.1.1",
99100
"karma-mocha": "^1.3.0",

src/components/alert/Alert.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
},
2121
type: {
2222
type: String,
23-
'default': 'success'
23+
'default': 'info'
2424
}
2525
},
2626
data () {

test/unit/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'es6-promise/auto'
12
import Vue from 'vue'
23
import * as uiv from '@src/components/index.js'
34

test/unit/specs/Alert.spec.js

Lines changed: 44 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,31 @@
11
import Vue from 'vue'
2+
import $ from 'jquery'
23
import Alert from '@src/components/alert/Alert.vue'
34
import AlertDoc from '@docs/pages/components/Alert.md'
45
import utils from './../utils'
56

7+
const DEFAULT_ALERT_CLASS = 'alert-info'
8+
69
describe('Alert', () => {
7-
let app
10+
let vm
11+
let $el
812

913
beforeEach(() => {
10-
app = new Vue({
11-
template: '<alert-doc ref="doc"/>',
12-
components: {AlertDoc}
13-
})
14+
let Constructor = Vue.extend(AlertDoc)
15+
vm = new Constructor().$mount()
16+
$el = $(vm.$el)
1417
})
1518

1619
afterEach(() => {
17-
try {
18-
app.$destroy()
19-
} catch (err) {
20-
// Silent
21-
}
20+
vm.$destroy()
21+
$el = null
2222
})
2323

24-
it('Should be able to add alert with no type', (done) => {
24+
const getDefaultAlertLength = () => {
25+
return $el.find(`.${DEFAULT_ALERT_CLASS}`).length
26+
}
27+
28+
it('Should be able to add alert with no type', () => {
2529
let res = Vue.compile('<alert>{{ msg }}</alert>')
2630
let vm = new Vue({
2731
data () {
@@ -34,62 +38,41 @@ describe('Alert', () => {
3438
staticRenderFns: res.staticRenderFns
3539
})
3640
vm.$mount()
37-
vm.$nextTick(() => {
38-
expect(vm.$el.className).to.equal('alert alert-success')
39-
vm.$destroy()
40-
done()
41-
})
41+
let $el = $(vm.$el)
42+
expect($el.hasClass('alert')).to.be.true
43+
expect($el.hasClass(DEFAULT_ALERT_CLASS)).to.be.true
44+
vm.$destroy()
4245
})
4346

44-
it('Alert can be closed', (done) => {
45-
let vm = app.$mount().$refs.doc
46-
vm.$nextTick(() => {
47-
let alertInfo = vm.$el.querySelector('#alert-test')
48-
expect(alertInfo).to.exist
49-
let closedBtn = alertInfo.querySelector('button.close')
50-
utils.triggerEvent(closedBtn, 'click')
51-
vm.$nextTick(() => {
52-
expect(vm.$el.querySelector('#alert-test')).not.exist
53-
done()
54-
})
55-
})
47+
it('Should be able to dismiss alerts', async () => {
48+
let alert = $el.find('#alert-test')
49+
expect(alert.length).to.equal(1)
50+
let closedBtn = alert.find('button.close')
51+
closedBtn.click()
52+
await vm.$nextTick()
53+
expect($el.find('#alert-test').length).to.equal(0)
5654
})
5755

58-
it('Can add a Alert', (done) => {
59-
let vm = app.$mount().$refs.doc
60-
vm.$nextTick(() => {
61-
let alertSuccess = vm.$el.querySelectorAll('.alert-success')
62-
let alertSuccessLengthBefore = alertSuccess.length
63-
let addAlertBtn = vm.$el.querySelector('#add-alert-2')
64-
utils.triggerEvent(addAlertBtn, 'click')
65-
vm.$nextTick(() => {
66-
alertSuccess = vm.$el.querySelectorAll('.alert-success')
67-
let alertSuccessLengthAfter = alertSuccess.length
68-
expect(alertSuccessLengthAfter).to.equal(alertSuccessLengthBefore + 1)
69-
done()
70-
})
71-
})
56+
it('Should be able to add dismissible alerts', async () => {
57+
let before = getDefaultAlertLength()
58+
let addAlertBtn = $el.find('#add-alert-1')
59+
addAlertBtn.click()
60+
await vm.$nextTick()
61+
let after = getDefaultAlertLength()
62+
expect(after).to.equal(before + 1)
7263
})
7364

74-
it('Can add a Alert had duration', (done) => {
75-
let vm = app.$mount().$refs.doc
65+
it('Should be able to add auto dismiss alerts', async () => {
7666
vm.duration = 1000
77-
vm.$nextTick(() => {
78-
let alertSuccess = vm.$el.querySelectorAll('.alert-success')
79-
let alertSuccessLengthBefore = alertSuccess.length
80-
let addAlertBtn = vm.$el.querySelector('#add-alert-1')
81-
utils.triggerEvent(addAlertBtn, 'click')
82-
vm.$nextTick(() => {
83-
alertSuccess = vm.$el.querySelectorAll('.alert-success')
84-
let alertSuccessLengthAfter = alertSuccess.length
85-
expect(alertSuccessLengthAfter).to.equal(alertSuccessLengthBefore + 1)
86-
setTimeout(() => {
87-
alertSuccess = vm.$el.querySelectorAll('.alert-success')
88-
alertSuccessLengthAfter = alertSuccess.length
89-
expect(alertSuccessLengthAfter).to.equal(alertSuccessLengthBefore)
90-
done()
91-
}, 1200)
92-
})
93-
})
67+
await vm.$nextTick()
68+
let before = getDefaultAlertLength()
69+
let addAlertBtn = $el.find('#add-alert-2')
70+
addAlertBtn.click()
71+
await vm.$nextTick()
72+
let after = getDefaultAlertLength()
73+
expect(after).to.equal(before + 1)
74+
await utils.sleep(vm.duration + 200)
75+
let _after = getDefaultAlertLength()
76+
expect(_after).to.equal(before)
9477
})
9578
})

test/unit/utils.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,10 @@ export default {
2222
? elm.dispatchEvent(evt)
2323
: elm.fireEvent('on' + name, evt)
2424
return elm
25+
},
26+
sleep (time) {
27+
return new Promise(resolve => {
28+
setTimeout(resolve, time)
29+
})
2530
}
2631
}

yarn.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3614,6 +3614,10 @@ joi@^6.7.x:
36143614
moment "2.x.x"
36153615
topo "1.x.x"
36163616

3617+
jquery@^3.2.1:
3618+
version "3.2.1"
3619+
resolved "https://registry.yarnpkg.com/jquery/-/jquery-3.2.1.tgz#5c4d9de652af6cd0a770154a631bba12b015c787"
3620+
36173621
js-base64@^2.1.9:
36183622
version "2.3.2"
36193623
resolved "https://registry.yarnpkg.com/js-base64/-/js-base64-2.3.2.tgz#a79a923666372b580f8e27f51845c6f7e8fbfbaf"

0 commit comments

Comments
 (0)