Skip to content

Instantly share code, notes, and snippets.

@MiracleBlue
Last active December 16, 2015 01:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MiracleBlue/0cc5f2bf6cbf340083bd to your computer and use it in GitHub Desktop.
Save MiracleBlue/0cc5f2bf6cbf340083bd to your computer and use it in GitHub Desktop.
Simple fuzzing unit testing example with Ember CLI and Ember Mocha
/* jshint expr:true */
import { expect } from 'chai';
import {
describe,
it
} from 'mocha';
import {
convertTimeTo12
} from 'digital-court-results/helpers/time-format';
import _ from 'lodash/lodash';
const {range, sample, times} = _;
const hoursAm = range(0, 11).map(num => num > 9 ? num.toString() : `0${num}`);
const hoursPm = range(13, 23).map(num => `${num}`);
const minutesRange = range(0, 59).map(num => `${num}`);
describe('TimeFormatHelper', function() {
// We run these tests multiple times to make sure a good sample of inputs
// gets run through the test process to weed out any odd bugs
times(10, index => {
it(`converts morning 24 hour time to 12 hour time with am suffix - round ${index + 1}`, function() {
const hourIn24 = sample(hoursAm);
const minuteIn24 = sample(minutesRange);
const timeIn24 = `${hourIn24}${minuteIn24}`;
const result = convertTimeTo12({timeIn24});
const [hourIn12, minuteWithSuffix] = result.split(':');
expect(parseInt(hourIn12)).to.equal(parseInt(hourIn24), `Hour in 12 (${hourIn12}) should equal hour in 24 (${hourIn24})`);
expect(minuteWithSuffix).to.contain('am');
});
});
times(10, index => {
it(`converts afternoon 24 hour time to 12 hour time with pm suffix - round ${index + 1}`, function() {
const hourIn24 = sample(hoursPm);
const minuteIn24 = sample(minutesRange);
const timeIn24 = `${hourIn24}${minuteIn24}`;
const result = convertTimeTo12({timeIn24});
const [hourIn12, minuteWithSuffix] = result.split(':');
const hourIn24ToCompare = parseInt(hourIn24) - 12;
const hourIn12ToCompare = parseInt(hourIn12);
expect(hourIn12ToCompare).to.equal(hourIn24ToCompare, `Hour in 12 (${hourIn12}) should be hour in 24 minus 12 (${hourIn24ToCompare})`);
expect(minuteWithSuffix).to.contain('pm');
});
});
it(`converts noon 24 hour time to 12 hour time with pm suffix`, function() {
const hourIn24 = '12';
const minuteIn24 = sample(minutesRange);
const timeIn24 = `${hourIn24}${minuteIn24}`;
const result = convertTimeTo12({timeIn24});
const [hourIn12, minuteWithSuffix] = result.split(':');
const hourIn24ToCompare = parseInt(hourIn24);
const hourIn12ToCompare = parseInt(hourIn12);
expect(hourIn12ToCompare).to.equal(hourIn24ToCompare, `Hour in 12 (${hourIn12}) should be hour in 24 (${hourIn24ToCompare})`);
expect(minuteWithSuffix).to.contain('pm');
});
});
import Ember from 'ember';
export function isNumeric(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
export function isValid24HourTime(time) {
const isValidTime = !!time &&
!Ember.isEmpty(time) &&
time.length === 4 &&
isNumeric(time);
return isValidTime;
}
// input 2310 -> output 11:10 pm
// input 0930 -> 9:30 am
export function convertTimeTo12({timeIn24, capitals}) {
let suffix = 'am';
let hours = parseInt(timeIn24.slice(0, 2));
const minutes = timeIn24.slice(2, 4);
if (hours >= 12) suffix = 'pm';
if (hours > 12) hours = hours - 12;
const timeIn12 = `${hours}:${minutes} ${suffix}`;
if (capitals) return timeIn12.toUpperCase();
return timeIn12;
}
export default Ember.Helper.extend({
compute([timeIn24], {capitals}) {
if (!isValid24HourTime(timeIn24)) return null;
const timeIn12 = convertTimeTo12({timeIn24, capitals});
return timeIn12;
}
});
@MiracleBlue
Copy link
Author

timeIn24 is expected to be a string like '0930' which will transform to '9:30 am' by the convertTimeTo12 function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment