Skip to content

Commit

Permalink
refactor formatDate filter
Browse files Browse the repository at this point in the history
  • Loading branch information
tomlovesgithub committed Nov 25, 2020
1 parent bc9faac commit 6acce7a
Show file tree
Hide file tree
Showing 3 changed files with 213 additions and 26 deletions.
45 changes: 27 additions & 18 deletions src/filters.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,34 @@
import { APPLICATION_STATUS, QUALIFYING_TEST } from '@/helpers/constants';

const formatDate = (value, type) => {
const objDate = new Date(value);
if (type && value){
switch (type) {
case 'month':
return `${objDate.toLocaleString('en-GB', { month: 'long' })} ${objDate.getUTCFullYear()}`;
case 'datetime':
return objDate.toLocaleString('en-GB');
case 'long':
return objDate.toLocaleDateString('en-GB', { year: 'numeric', month: 'long', day: 'numeric' });
case 'longdatetime':
return objDate.toLocaleDateString('en-GB', { year: 'numeric', month: 'long', day: 'numeric', hour: 'numeric', minute: 'numeric' });
default:
return objDate.toLocaleDateString('en-GB');
if (!isNaN(new Date(value).valueOf()) && value !== null) {
if (!type) {
if (value instanceof Date) {
value = value.toLocaleDateString('en-GB');
} else if (value instanceof Array) {
value = new Date(value).toLocaleDateString('en-GB');
} else {
value = new Date(value).toLocaleDateString('en-GB');
}
} else {
value = new Date(value);
switch (type) {
case 'month':
value = `${value.toLocaleString('en-GB', { month: 'long' })} ${value.getUTCFullYear()}`;
break;
case 'datetime':
value = value.toLocaleString('en-GB');
break;
case 'long':
value = value.toLocaleDateString('en-GB', { year: 'numeric', month: 'long', day: 'numeric' });
break;
case 'longdatetime':
value = value.toLocaleDateString('en-GB', { year: 'numeric', month: 'long', day: 'numeric', hour: 'numeric', minute: 'numeric' });
break;
}
}
} else if (value) {
return objDate.toLocaleDateString('en-Gb');
} else {
return new Date([]).toLocaleDateString('en-Gb');
}
}
return value;
};

const formatEstimatedDate = (value) => {
Expand Down
179 changes: 179 additions & 0 deletions tests/journeys/page-titles.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
import router from '@/router';
import store from '@/store';

const routes = [
['exercise-new', 'Create An Exercise'],
['exercise-show-overview', 'Exercise Details | Overview'],
['exercise-show-applications', 'Exercise Details | Applications'],
['exercise-show-contacts', 'Exercise Details | Contacts'],
['exercise-show-timeline', 'Exercise Details | Timeline'],
['exercise-show-shortlisting', 'Exercise Details | Shortlisting'],
['exercise-show-vacancy', 'Exercise Details | Vacancy information'],
['exercise-show-eligibility', 'Exercise Details | Eligibility information'],
['exercise-show-independent-assessments', 'Exercise Details | Independent Assessments'],
['exercise-edit-contacts', 'Contacts'],
['exercise-edit-shortlisting', 'Shortlisting Methods'],
['exercise-edit-timeline', 'Timeline'],
['exercise-edit-eligibility', 'Eligibility Information'],
['exercise-edit-vacancy', 'Vacancy Information'],
['exercise-edit-name', 'Update exercise name'],
['exercise-not-found', 'Exercise Not Found'],
['page-not-found', 'Page Not Found'],
['exercise-reports-character-checks', 'Character Checks'],
['exercise-reports-character-issues', 'Character Issues'],
['exercise-reports-diversity-dashboard', 'Diversity Dashboard'],
['exercise-reports-eligibility-issues', 'Eligibility Issues'],
['exercise-reports-education-and-career-history', 'Education and Career History'],
['exercise-reports-jo-handover-report', 'JO Handover Report'],
['exercise-reports-statutory-consultation-table', 'Statutory Consultation Table'],
['exercise-reports-reasonable-adjustments', 'Reasonable Adjustments'],
];

xdescribe('Page titles', () => {

const pushSpy = jest.spyOn(router, 'push');
const user = {
uid: 'abc123',
email: 'user@judicialappointments.digital',
};

describe('router', () => {
beforeEach(() => {
router.push({ name: 'sign-in' });
store.dispatch('auth/setCurrentUser', user);
pushSpy.mockClear();
});

});

describe('sign in', () => {
beforeEach(() => {
router.push({ name: 'sign-in' });
});

it('contains Sign In', () => {
expect(document.title).toContain('Sign In');
});

it('contains Judicial Appointments Commission', () => {
expect(document.title).toContain('Judicial Appointments Commission');
});
});

describe.each(routes)('%s', (routeName, routeTitle) => {
beforeEach(() => {
store.dispatch('auth/setCurrentUser', user);
router.push({ name: routeName, params: { id: 123 } });
});

it(`contains ${routeTitle}`, () => {
expect(document.title).toContain(routeTitle);
});

it('contains Judicial Appointments Commission', () => {
expect(document.title).toContain('Judicial Appointments Commission');
});
});
});

// import { shallowMount, createLocalVue } from '@vue/test-utils';
// import App from '@/App';
// import Router from 'vue-router';
// import Vuex from 'vuex';

// const routes = [
// ['exercise-new', 'Create An Exercise'],
// ['exercise-show-overview', 'Exercise Details | Overview'],
// ['exercise-show-applications', 'Exercise Details | Applications'],
// ['exercise-show-contacts', 'Exercise Details | Contacts'],
// ['exercise-show-timeline', 'Exercise Details | Timeline'],
// ['exercise-show-shortlisting', 'Exercise Details | Shortlisting'],
// ['exercise-show-vacancy', 'Exercise Details | Vacancy information'],
// ['exercise-show-eligibility', 'Exercise Details | Eligibility information'],
// ['exercise-show-independent-assessments', 'Exercise Details | Independent Assessments'],
// ['exercise-edit-contacts', 'Contacts'],
// ['exercise-edit-shortlisting', 'Shortlisting Methods'],
// ['exercise-edit-timeline', 'Timeline'],
// ['exercise-edit-eligibility', 'Eligibility Information'],
// ['exercise-edit-vacancy', 'Vacancy Information'],
// ['exercise-edit-name', 'Update exercise name'],
// ['exercise-not-found', 'Exercise Not Found'],
// ['page-not-found', 'Page Not Found'],
// ['exercise-reports-character-checks', 'Character Checks'],
// ['exercise-reports-character-issues', 'Character Issues'],
// ['exercise-reports-diversity-dashboard', 'Diversity Dashboard'],
// ['exercise-reports-eligibility-issues', 'Eligibility Issues'],
// ['exercise-reports-education-and-career-history', 'Education and Career History'],
// ['exercise-reports-jo-handover-report', 'JO Handover Report'],
// ['exercise-reports-statutory-consultation-table', 'Statutory Consultation Table'],
// ['exercise-reports-reasonable-adjustments', 'Reasonable Adjustments'],
// ];

// xdescribe('Page titles', () => {
// let router;
// let store;

// beforeEach(() => {
// const localVue = createLocalVue();
// localVue.use(Router);
// localVue.use(Vuex);

// router = require('@/router').default;
// store = require('@/store').default;
// window.scrollTo = () => {};
// shallowMount(App, {
// localVue,
// router,
// store,
// });
// });

// const user = {
// uid: 'abc123',
// email: 'user@judicialappointments.digital',
// };

// describe('sign in', () => {
// beforeEach(() => {
// router.push({ name: 'sign-in' });
// });

// it('contains Sign In', () => {
// expect(document.title).toContain('Sign In');
// });

// it('contains Judicial Appointments Commission', () => {
// expect(document.title).toContain('Judicial Appointments Commission');
// });
// });

// describe('dashboard', () => {
// beforeEach(() => {
// store.dispatch('auth/setCurrentUser', user);
// router.push({ name: 'dashboard' });
// });

// it('contains Dashboard', () => {
// expect(document.title).toContain('Dashboard');
// });

// it('contains Judicial Appointments Commission', () => {
// expect(document.title).toContain('Judicial Appointments Commission');
// });
// });

// describe.each(routes)('%s', (routeName, routeTitle) => {
// beforeEach(() => {
// store.dispatch('auth/setCurrentUser', user);
// router.push({ name: routeName, params: { id: 123 } });
// });

// it(`contains ${routeTitle}`, () => {
// expect(document.title).toContain(routeTitle);
// });

// it('contains Judicial Appointments Commission', () => {
// expect(document.title).toContain('Judicial Appointments Commission');
// });
// });
// });
15 changes: 7 additions & 8 deletions tests/unit/filters/formatDate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('Format Date', () => {
[[1,1,2003], new Date([1,1,2003]).toLocaleDateString('en-GB')],
[1600000000000, new Date(1600000000000).toLocaleDateString('en-GB')], // epoch/UTC time gives number
];

it.each(validTypes)('when given %s returns %o', async (value, result) => {
expect(formatDate(value)).toBe(result);
});
Expand All @@ -26,18 +26,17 @@ describe('Format Date', () => {
expect(formatDate(value, type)).toBe(result);
});
});

describe('invalid date', () => {

const invalidTypes = [
['string'],
[null],
[undefined],
[[]],
['string', 'string'],
[null, null],
[undefined, undefined],
[[], []],
];

it.each(invalidTypes)('when given %o', (value) => {
expect(formatDate(value)).toBe('Invalid Date');
it.each(invalidTypes)('when given %o', (value, result) => {
expect(formatDate(value)).toEqual(result);
});
});

Expand Down

0 comments on commit 6acce7a

Please sign in to comment.