Skip to content

Commit 1805923

Browse files
fix: wrong time display at 12 am/pm (#10)
* fix: wrong time display at 12 am/pm * test: add tests for 12am/pm
1 parent 5c72db5 commit 1805923

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/Clock.spec.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,18 @@ describe("Clock.vue", () => {
159159
expect(wrapper.find(".clock__hour")[0].text()).toContain("09");
160160
expect(wrapper.find(".clock__ampm")[0].text()).toContain("pm");
161161
});
162+
163+
it("displays 12pm (noon/midday) in twelveHour mode", () => {
164+
clock.tick(12 * hours);
165+
const wrapper = mount(Clock, { propsData: { twelveHour: true } });
166+
expect(wrapper.find(".clock__hour")[0].text()).toContain("12");
167+
expect(wrapper.find(".clock__ampm")[0].text()).toContain("pm");
168+
});
169+
170+
it("displays 12am (midnight) in twelveHour mode", () => {
171+
clock.tick(0 * hours);
172+
const wrapper = mount(Clock, { propsData: { twelveHour: true } });
173+
expect(wrapper.find(".clock__hour")[0].text()).toContain("12");
174+
expect(wrapper.find(".clock__ampm")[0].text()).toContain("am");
175+
});
162176
});

src/Clock.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ const getMinutes = () => padZero(getDate().getMinutes());
4040
4141
const getHour = twelveHour => {
4242
let hours = getDate().getHours();
43-
if (twelveHour && hours > 12) {
44-
hours = hours - 12;
43+
if (twelveHour) {
44+
hours = hours % 12 || 12;
4545
}
4646
return padZero(hours);
4747
};
4848
49-
const getAmPm = () => (getDate().getHours() > 12 ? "pm" : "am");
49+
const getAmPm = () => (getDate().getHours() >= 12 ? "pm" : "am");
5050
5151
export default {
5252
name: "vue-digital-clock",

0 commit comments

Comments
 (0)