Skip to content

Commit 7bada8d

Browse files
committed
[New] Add blink prop to Clock component
1 parent b5ad1ec commit 7bada8d

File tree

3 files changed

+51
-10
lines changed

3 files changed

+51
-10
lines changed

Clock.vue

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<template>
22
<time class="clock">
3-
<span class="clock__hour">{{ hours }}</span>:<span class="clock__minutes">{{ minutes }}</span>
3+
<span class="clock__hour">{{ hours }}</span><!--
4+
--><span v-if="!blink || seconds % 2 === 0">:</span><!--
5+
--><span v-else>&nbsp;</span><!--
6+
--><span class="clock__minutes">{{ minutes }}</span>
47
</time>
58
</template>
69

@@ -16,6 +19,10 @@ function getDate() {
1619
return new Date();
1720
}
1821
22+
function getSeconds() {
23+
return getDate().getSeconds();
24+
}
25+
1926
function getMinutes() {
2027
return padZero(getDate().getMinutes());
2128
}
@@ -27,11 +34,14 @@ function getHour() {
2734
module.exports = {
2835
name: 'clock',
2936
37+
props: ['blink'],
38+
3039
data: function data() {
3140
return {
3241
ticker: null,
3342
minutes: getMinutes(),
3443
hours: getHour(),
44+
seconds: getSeconds(),
3545
};
3646
},
3747
@@ -41,11 +51,13 @@ module.exports = {
4151
this.ticker = setInterval(function ticker() {
4252
_this.minutes = getMinutes();
4353
_this.hours = getHour();
54+
_this.seconds = getSeconds();
4455
}, 1000);
4556
},
4657
4758
destroyed: function destroyed() {
4859
clearInterval(this.ticker);
4960
},
61+
5062
};
5163
</script>

example/App.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<Clock />
2+
<Clock :blink="true"/>
33
</template>
44

55
<script>

test/unit/specs/Clock.spec.js

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,47 +19,76 @@ describe('Clock.vue', () => {
1919
clock = sinon.useFakeTimers(new Date(2016, 2, 15).getTime());
2020
clock.tick(21 * hours);
2121
const wrapper = mount(Clock);
22-
expect(wrapper.find('.clock__hour')[0].text()).to.equal('21');
22+
expect(wrapper.find('.clock__hour')[0].text()).to.contain('21');
2323
});
2424

2525
it('renders current hour with padded 0', () => {
2626
clock = sinon.useFakeTimers(new Date(2016, 2, 15).getTime());
2727
clock.tick(3 * hours);
2828
const wrapper = mount(Clock);
29-
expect(wrapper.find('.clock__hour')[0].text()).to.equal('03');
29+
expect(wrapper.find('.clock__hour')[0].text()).to.contain('03');
3030
});
3131

3232
it('Updates hours when changed', () => {
3333
clock = sinon.useFakeTimers(new Date(2016, 2, 15).getTime());
3434
clock.tick(13 * hours);
3535
const wrapper = mount(Clock);
36-
expect(wrapper.find('.clock__hour')[0].text()).to.equal('13');
36+
expect(wrapper.find('.clock__hour')[0].text()).to.contain('13');
3737
clock.tick(14 * hours);
38-
setTimeout(() => expect(wrapper.find('.clock__hour')[0].text()).to.equal('14'), 1000);
38+
setTimeout(() => expect(wrapper.find('.clock__hour')[0].text()).to.contain('14'), 1000);
3939
});
4040

4141
it('renders current minutes', () => {
4242
clock = sinon.useFakeTimers(new Date(2016, 2, 15).getTime());
4343
clock.tick(30 * minutes);
4444
const wrapper = mount(Clock);
45-
expect(wrapper.find('.clock__minutes')[0].text()).to.equal('30');
45+
expect(wrapper.find('.clock__minutes')[0].text()).to.contain('30');
4646
});
4747

4848
it('renders current minutes with padded 0', () => {
4949
clock = sinon.useFakeTimers(new Date(2016, 2, 15).getTime());
5050
clock.tick(3 * minutes);
5151
const wrapper = mount(Clock);
52-
expect(wrapper.find('.clock__minutes')[0].text()).to.equal('03');
52+
expect(wrapper.find('.clock__minutes')[0].text()).to.contain('03');
5353
});
5454

5555
it('Updates minutes when changed', () => {
5656
clock = sinon.useFakeTimers(new Date(2016, 2, 15).getTime());
5757
clock.tick(3 * minutes);
5858
const wrapper = mount(Clock);
59-
expect(wrapper.find('.clock__minutes')[0].text()).to.equal('03');
59+
expect(wrapper.find('.clock__minutes')[0].text()).to.contain('03');
6060
clock.tick(4 * minutes);
61-
setTimeout(() => expect(wrapper.find('.clock__minutes')[0].text()).to.equal('04'), 1000);
61+
setTimeout(() => expect(wrapper.find('.clock__minutes')[0].text()).to.contain('04'), 1000);
6262
});
63+
64+
it('displays colon when not passed blink prop and seconds are even', () => {
65+
clock = sinon.useFakeTimers(new Date(2016, 2, 15).getTime());
66+
clock.tick(seconds * 2);
67+
const wrapper = mount(Clock);
68+
expect(wrapper.text()).to.contain(':');
69+
});
70+
71+
it('displays colon when not passed blink prop and seconds are odd', () => {
72+
clock = sinon.useFakeTimers(new Date(2016, 2, 15).getTime());
73+
clock.tick(seconds);
74+
const wrapper = mount(Clock);
75+
expect(wrapper.text()).to.contain(':');
76+
});
77+
78+
it('displays colon when passed blink prop and seconds are even', () => {
79+
clock = sinon.useFakeTimers(new Date(2016, 2, 15).getTime());
80+
clock.tick(seconds * 2);
81+
const wrapper = mount(Clock, { propsData: { blink: true } });
82+
expect(wrapper.text()).to.contain(':');
83+
});
84+
85+
it('does not display colon when passed blink prop and seconds are odd', () => {
86+
clock = sinon.useFakeTimers(new Date(2016, 2, 15).getTime());
87+
clock.tick(seconds);
88+
const wrapper = mount(Clock, { propsData: { blink: true } });
89+
expect(wrapper.text()).to.not.contain(':');
90+
});
91+
6392
it('Calls clear input with vm.ticker when component is destroyed', () => {
6493
const stub = sinon.stub();
6594
window.clearInterval = stub;

0 commit comments

Comments
 (0)