Skip to content

Commit

Permalink
When no period is specified, make assumptions about what period
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenreddek committed Nov 9, 2017
1 parent a407933 commit 370d6d7
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 18 deletions.
50 changes: 43 additions & 7 deletions nightwatch-tests/suites/simple.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ const hourListSelector = ".elm-time-picker-panel-select ul:first-child";
const minuteListSelector = ".elm-time-picker-panel-select ul:first-child";
const secondListSelector = ".elm-time-picker-panel-select ul:first-child";

const hour12Selector = hourListSelector + " li:nth-child(1)";
const hour5Selector = hourListSelector + " li:nth-child(6)";
const hour7Selector = hourListSelector + " li:nth-child(8)";
const hour11Selector = hourListSelector + " li:nth-child(12)";

const selectedHoursSelector = "#selected-hours";
const selectedMinutesSelector = "#selected-minutes";
Expand Down Expand Up @@ -67,14 +70,27 @@ const sendSlowly = (client, selector, keys) => {
}

module.exports = {
'When selecting hour 5 with the mouse, it should appear in the text input': (client) => {
'When selecting hour 5 with the mouse, it should default the period to PM and appear in the text input': (client) => {
client.url(url);
client.expect.element(textInputSelector).to.be.present.before(1000);
client.click(textInputSelector);
client.expect.element(hour5Selector).to.be.present.before(1000);
client.click(hour5Selector);
client.expect.element(textInputSelector).value.to.equal("5:00:00 AM").before(1000);
client.expect.element(selectedHoursSelector).text.to.equal(5);
client.expect.element(textInputSelector).value.to.equal("5:00:00 PM").before(1000);
client.expect.element(selectedHoursSelector).text.to.equal(17);
client.expect.element(selectedMinutesSelector).text.to.equal(0);
client.expect.element(selectedSecondsSelector).text.to.equal(0);
client.end();
},

'When selecting hour 12 with the mouse, it should default the period to PM and appear in the text input': (client) => {
client.url(url);
client.expect.element(textInputSelector).to.be.present.before(1000);
client.click(textInputSelector);
client.expect.element(hour12Selector).to.be.present.before(1000);
client.click(hour12Selector);
client.expect.element(textInputSelector).value.to.equal("12:00:00 PM").before(1000);
client.expect.element(selectedHoursSelector).text.to.equal(12);
client.expect.element(selectedMinutesSelector).text.to.equal(0);
client.expect.element(selectedSecondsSelector).text.to.equal(0);
client.end();
Expand All @@ -87,6 +103,21 @@ module.exports = {
client.setValue(textInputSelector, "1:00:00 AM");
client.expect.element(hour5Selector).to.be.present.before(1000);
client.click(hour5Selector);
client.expect.element(textInputSelector).value.to.equal("5:00:00 PM").before(1000);
client.expect.element(selectedHoursSelector).text.to.equal(17);
client.expect.element(selectedMinutesSelector).text.to.equal(0);
client.expect.element(selectedSecondsSelector).text.to.equal(0);
client.end();
},

'When a valid time has been entered, and then selecting an hour with the mouse, the selected hour should use the period from the input time': (client) => {
client.url(url);
client.expect.element(textInputSelector).to.be.present.before(1000);
client.click(textInputSelector);
sendSlowly(client,textInputSelector, [..."1:00:00 AM", client.Keys.ENTER]);
client.expect.element(textInputSelector).value.to.equal("1:00:00 AM").before(1000);
client.expect.element(hour5Selector).to.be.present.before(1000);
client.click(hour5Selector);
client.expect.element(textInputSelector).value.to.equal("5:00:00 AM").before(1000);
client.expect.element(selectedHoursSelector).text.to.equal(5);
client.expect.element(selectedMinutesSelector).text.to.equal(0);
Expand All @@ -101,7 +132,7 @@ module.exports = {
client.click(textInputSelector);
client.expect.element(hour5Selector).to.be.present.before(1000);
client.click(hour5Selector);
client.expect.element(textInputSelector).value.to.equal("5:00:00 AM").before(1000);
client.expect.element(textInputSelector).value.to.equal("5:00:00 PM").before(1000);

client.click(textInputSelector);
client.clearValue(textInputSelector);
Expand All @@ -124,16 +155,16 @@ module.exports = {
client.expect.element(hour5Selector).to.be.present.before(1000);
client.click(hour5Selector);

const valid_time = "5:00:00 AM"
const valid_time = "5:00:00 PM"

client.expect.element(textInputSelector).value.to.equal(valid_time).before(1000);

client.click(textInputSelector);
clearWithBackspace(client, textInputSelector, valid_time.length);
sendSlowly(client,textInputSelector, [..."27:00:00 AM", client.Keys.ENTER]);

client.expect.element(textInputSelector).value.to.equal("5:00:00 AM").before(1000);
client.expect.element(selectedHoursSelector).text.to.equal(5);
client.expect.element(textInputSelector).value.to.equal(valid_time).before(1000);
client.expect.element(selectedHoursSelector).text.to.equal(17);
client.expect.element(selectedMinutesSelector).text.to.equal(0);
client.expect.element(selectedSecondsSelector).text.to.equal(0);

Expand All @@ -149,6 +180,11 @@ module.exports = {
...testValidManualInput("12 AM", "12:00:00 AM", 0, 0, 0),
...testValidManualInput("0", "12:00:00 AM", 0, 0, 0),
...testValidManualInput("0PM", "12:00:00 PM", 12, 0, 0),
...testValidManualInput("7", "7:00:00 AM", 7, 0, 0),
...testValidManualInput("11", "11:00:00 AM", 11, 0, 0),
...testValidManualInput("12", "12:00:00 PM", 12, 0, 0),
...testValidManualInput("1", "1:00:00 PM", 13, 0, 0),
...testValidManualInput("6", "6:00:00 PM", 18, 0, 0),

...testInvalidManualInput("blarg"),
...testInvalidManualInput("b 12:00:00 PM b"),
Expand Down
33 changes: 22 additions & 11 deletions src/TimePicker.elm
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,13 @@ update settings msg (TimePicker ({ value } as model)) =

SelectHour hours ->
let
timeToUpdate =
Maybe.withDefault defaultTime value

updatedTime =
Just { timeToUpdate | hours = hours }
case value of
Just time ->
Just { time | hours = hours }

Nothing ->
Just (defaultPeriodIn12HourFormat settings True { defaultTime | hours = hours })
in
( TimePicker { model | value = updatedTime, inputText = Nothing }, Changed updatedTime )

Expand Down Expand Up @@ -244,6 +246,21 @@ setTimeWithPeriod period time =
{ time | hours = time.hours + 12 }



-- 7 AM - 11 AM
-- 12 PM - 6 PM


defaultPeriodIn12HourFormat : Settings -> Bool -> Time -> Time
defaultPeriodIn12HourFormat settings zeroIs12 time =
if settings.use24Hours then
time
else if (time.hours > 0 || (time.hours == 0 && zeroIs12)) && time.hours <= 6 then
{ time | hours = time.hours + 12 }
else
time


period : Time -> Period
period time =
if time.hours >= 12 then
Expand Down Expand Up @@ -312,17 +329,11 @@ parseTimeParts settings period timeParts =
++ partSetter settings.showMinutes setMinutes
++ partSetter settings.showSeconds setSeconds

setToMidnightIf12HourFormatAt12 time =
if (not settings.use24Hours) && time.hours == 12 then
{ time | hours = 0 }
else
time

withPeriod time =
if time.hours >= 0 && time.hours <= 12 then
period
|> Maybe.map ((flip setTimeWithPeriod) time)
|> Maybe.withDefault (setToMidnightIf12HourFormatAt12 time)
|> Maybe.withDefault (defaultPeriodIn12HourFormat settings False time)
else
time
in
Expand Down

0 comments on commit 370d6d7

Please sign in to comment.