Skip to content
This repository was archived by the owner on Jan 16, 2019. It is now read-only.

Commit

Permalink
ES6-ify + Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yangshun committed Oct 17, 2016
1 parent e977334 commit 462fea9
Show file tree
Hide file tree
Showing 19 changed files with 742 additions and 135 deletions.
10 changes: 10 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"presets": ["es2015"],
"env": {
"commonjs": {
"plugins": [
["transform-es2015-modules-commonjs", { "loose": true }]
]
}
}
}
38 changes: 38 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
module.exports = {
parser: 'babel-eslint',
root: true,
extends: [
'airbnb-base',
'plugin:flowtype/recommended',
],
env: {
browser: true,
node: true,
},
globals: {
expect: true,
it: true,
},
plugins: [
'flowtype',
'import',
],
settings: {
'import/resolver': {
webpack: {
config: 'webpack.config.js',
},
},
},
rules: {
'arrow-body-style': 'off',
'arrow-parens': ['error', 'as-needed', { requireForBlockBody: true }],
'max-len': ['error', 120],
'import/extensions': ['error', 'always',
{
js: 'never',
}
],
'linebreak-style': 'off',
},
};
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,8 @@ node_modules

# Users Environment Variables
.lock-wscript

# Build
es
dist
lib
15 changes: 15 additions & 0 deletions .jestconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"coverageDirectory": "coverage",
"moduleDirectories": ["node_modules"],
"moduleNameMapper": {
"^.+\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga|bmp|ico|yml)$": "<rootDir>/__mocks__/fileMock.js",
"^.+\\.(css|scss)$": "<rootDir>/__mocks__/styleMock.js"
},
"modulePaths": [
"src"
],
"collectCoverageFrom": [
"src/**/*.js"
],
"testRegex": "(/__tests__/.*\\.test.js)$"
}
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
6.6.0
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
language: node_js
node_js:
- '6'
1 change: 1 addition & 0 deletions __mocks__/fileMock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = "test-file-stub";
1 change: 1 addition & 0 deletions __mocks__/styleMock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {};
5 changes: 5 additions & 0 deletions __tests__/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
env: {
jest: true,
}
};
167 changes: 167 additions & 0 deletions __tests__/academicCalendar.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/* eslint-disable no-console */
import {
acadYearStartDates,
getAcadYear,
getAcadSem,
getAcadWeekName,
getAcadWeekInfo,
} from '../src/academicCalendar';

console.warn = jest.genMockFn();

describe('acadYearStartDates', () => {
it('has the start dates of 6 academic years', () => {
expect(Object.keys(acadYearStartDates).length).toBe(6);
});
});

describe('getAcadYear', () => {
it('determines the correct start date for supported dates', () => {
expect(getAcadYear(new Date('July 31, 2016')).year).toBe('15/16');
expect(getAcadYear(new Date('August 1, 2016')).year).toBe('16/17');
expect(getAcadYear(new Date('October 17, 2016')).year).toBe('16/17');
expect(getAcadYear(new Date('October 17, 2017')).year).toBe('17/18');
});

it('gives defaults for unsupported dates', () => {
// Earlier than supported year range.
expect(getAcadYear(new Date('October 17, 2006'))).toBe(null);
// Later than supported year range.
expect(getAcadYear(new Date('October 17, 2106')).year).toBe('19/20');
});
});

describe('getAcadSem', () => {
it('determines the correct semester for supported weeks', () => {
expect(getAcadSem(1)).toBe('Semester 1');
expect(getAcadSem(15)).toBe('Semester 1');
expect(getAcadSem(23)).toBe('Semester 1');
expect(getAcadSem(24)).toBe('Semester 2');
expect(getAcadSem(32)).toBe('Semester 2');
expect(getAcadSem(40)).toBe('Semester 2');
expect(getAcadSem(41)).toBe('Special Term I');
expect(getAcadSem(43)).toBe('Special Term I');
expect(getAcadSem(46)).toBe('Special Term I');
expect(getAcadSem(47)).toBe('Special Term II');
expect(getAcadSem(50)).toBe('Special Term II');
expect(getAcadSem(52)).toBe('Special Term II');
});

it('gives null for unsupported weeks', () => {
expect(getAcadSem(-1)).toBe(null);
expect(getAcadSem(0)).toBe(null);
expect(getAcadSem(54)).toBe(null);
expect(getAcadSem(1000)).toBe(null);
expect(console.warn).toBeCalled();
});
});

describe('getAcadWeekName', () => {
it('determines the correct semester for supported weeks', () => {
expect(getAcadWeekName(1)).toEqual({ weekType: 'Instructional', weekNumber: 1 });
expect(getAcadWeekName(3)).toEqual({ weekType: 'Instructional', weekNumber: 3 });
expect(getAcadWeekName(7)).toEqual({ weekType: 'Recess', weekNumber: null });
expect(getAcadWeekName(8)).toEqual({ weekType: 'Instructional', weekNumber: 7 });
expect(getAcadWeekName(11)).toEqual({ weekType: 'Instructional', weekNumber: 10 });
expect(getAcadWeekName(15)).toEqual({ weekType: 'Reading', weekNumber: null });
expect(getAcadWeekName(16)).toEqual({ weekType: 'Examination', weekNumber: 1 });
expect(getAcadWeekName(17)).toEqual({ weekType: 'Examination', weekNumber: 2 });
});

it('gives null for unsupported weeks', () => {
expect(getAcadWeekName(-1)).toBe(null);
expect(getAcadWeekName(0)).toBe(null);
expect(getAcadWeekName(18)).toBe(null);
expect(getAcadWeekName(1000)).toBe(null);
expect(console.warn).toBeCalled();
});
});

describe('getAcadWeekInfo', () => {
it('determines the correct week info for supported weeks', () => {
expect(getAcadWeekInfo(new Date('August 1, 2016'))).toEqual({
year: '16/17', sem: 'Semester 1', type: 'Orientation', num: null,
});
expect(getAcadWeekInfo(new Date('August 8, 2016'))).toEqual({
year: '16/17', sem: 'Semester 1', type: 'Instructional', num: 1,
});
expect(getAcadWeekInfo(new Date('August 14, 2016'))).toEqual({
year: '16/17', sem: 'Semester 1', type: 'Instructional', num: 1,
});
expect(getAcadWeekInfo(new Date('September 12, 2016'))).toEqual({
year: '16/17', sem: 'Semester 1', type: 'Instructional', num: 6,
});
expect(getAcadWeekInfo(new Date('September 19, 2016'))).toEqual({
year: '16/17', sem: 'Semester 1', type: 'Recess', num: null,
});
expect(getAcadWeekInfo(new Date('September 27, 2016'))).toEqual({
year: '16/17', sem: 'Semester 1', type: 'Instructional', num: 7,
});
expect(getAcadWeekInfo(new Date('November 11, 2016'))).toEqual({
year: '16/17', sem: 'Semester 1', type: 'Instructional', num: 13,
});
expect(getAcadWeekInfo(new Date('November 14, 2016'))).toEqual({
year: '16/17', sem: 'Semester 1', type: 'Reading', num: null,
});
expect(getAcadWeekInfo(new Date('November 21, 2016'))).toEqual({
year: '16/17', sem: 'Semester 1', type: 'Examination', num: 1,
});
expect(getAcadWeekInfo(new Date('December 2, 2016'))).toEqual({
year: '16/17', sem: 'Semester 1', type: 'Examination', num: 2,
});
expect(getAcadWeekInfo(new Date('December 7, 2016'))).toEqual({
year: '16/17', sem: 'Semester 1', type: 'Vacation', num: 1,
});
expect(getAcadWeekInfo(new Date('December 28, 2016'))).toEqual({
year: '16/17', sem: 'Semester 1', type: 'Vacation', num: 4,
});
expect(getAcadWeekInfo(new Date('January 4, 2017'))).toEqual({
year: '16/17', sem: 'Semester 1', type: 'Vacation', num: 5,
});
expect(getAcadWeekInfo(new Date('January 9, 2017'))).toEqual({
year: '16/17', sem: 'Semester 2', type: 'Instructional', num: 1,
});
expect(getAcadWeekInfo(new Date('February 14, 2017'))).toEqual({
year: '16/17', sem: 'Semester 2', type: 'Instructional', num: 6,
});
expect(getAcadWeekInfo(new Date('February 21, 2017'))).toEqual({
year: '16/17', sem: 'Semester 2', type: 'Recess', num: null,
});
expect(getAcadWeekInfo(new Date('February 28, 2017'))).toEqual({
year: '16/17', sem: 'Semester 2', type: 'Instructional', num: 7,
});
expect(getAcadWeekInfo(new Date('April 11, 2017'))).toEqual({
year: '16/17', sem: 'Semester 2', type: 'Instructional', num: 13,
});
expect(getAcadWeekInfo(new Date('April 18, 2017'))).toEqual({
year: '16/17', sem: 'Semester 2', type: 'Reading', num: null,
});
expect(getAcadWeekInfo(new Date('April 25, 2017'))).toEqual({
year: '16/17', sem: 'Semester 2', type: 'Examination', num: 1,
});
expect(getAcadWeekInfo(new Date('May 2, 2017'))).toEqual({
year: '16/17', sem: 'Semester 2', type: 'Examination', num: 2,
});
expect(getAcadWeekInfo(new Date('May 9, 2017'))).toEqual({
year: '16/17', sem: 'Special Term I', type: 'Instructional', num: 1,
});
expect(getAcadWeekInfo(new Date('June 6, 2017'))).toEqual({
year: '16/17', sem: 'Special Term I', type: 'Instructional', num: 5,
});
expect(getAcadWeekInfo(new Date('June 13, 2017'))).toEqual({
year: '16/17', sem: 'Special Term I', type: 'Instructional', num: 6,
});
expect(getAcadWeekInfo(new Date('June 20, 2017'))).toEqual({
year: '16/17', sem: 'Special Term II', type: 'Instructional', num: 1,
});
expect(getAcadWeekInfo(new Date('July 25, 2017'))).toEqual({
year: '16/17', sem: 'Special Term II', type: 'Instructional', num: 6,
});
expect(getAcadWeekInfo(new Date('August 4, 2017'))).toEqual({
year: '16/17', sem: null, type: 'Vacation', num: null,
});
expect(getAcadWeekInfo(new Date('August 7, 2017'))).toEqual({
year: '17/18', sem: 'Semester 1', type: 'Orientation', num: null,
});
});
});
124 changes: 0 additions & 124 deletions academicCalendar.js

This file was deleted.

2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"nusmods"
],
"description": "A lightweight library with helpful functions for NUS-related matters.",
"main": "main.js",
"main": "lib/index.js",
"keywords": [
"NUS",
"modifications",
Expand Down
Loading

0 comments on commit 462fea9

Please sign in to comment.