Skip to content

Commit

Permalink
Merge 0c89926 into a5db037
Browse files Browse the repository at this point in the history
  • Loading branch information
Geo Galagaran committed Oct 27, 2018
2 parents a5db037 + 0c89926 commit 38a9943
Show file tree
Hide file tree
Showing 7 changed files with 557 additions and 349 deletions.
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"test:coverage": "jest --coverage --coverageReporters=text-lcov",
"test:coverage:coveralls": "yarn test:coverage | coveralls",
"linter:init": "eslint --init",
"build": "rm -rf ./lib && babel ./src -d ./lib",
"build": "rm -rf ./lib && babel ./src -d ./lib --ignore '**/*.test.js'",
"commit": "git-cz",
"travis-deploy-once": "travis-deploy-once",
"semantic-release": "semantic-release"
Expand Down Expand Up @@ -39,8 +39,8 @@
"react": ">=0.14.x"
},
"dependencies": {
"prop-types": ">=15",
"date-fns": "1.x"
"date-fns": "1.x",
"prop-types": ">=15"
},
"devDependencies": {
"babel-cli": "6.26.0",
Expand All @@ -52,8 +52,6 @@
"babel-preset-stage-1": "6.24.1",
"commitizen": "2.9.6",
"cz-conventional-changelog": "2.1.0",
"enzyme": "3.3.0",
"enzyme-adapter-react-16": "1.1.1",
"eslint": "4.19.1",
"eslint-config-standard": "11.0.0",
"eslint-plugin-import": "2.11.0",
Expand All @@ -66,12 +64,16 @@
"prettier": "1.12.1",
"react": "16.3.2",
"react-dom": "16.3.2",
"react-testing-library": "5.0.0",
"semantic-release": "^15.4.1",
"travis-deploy-once": "^5.0.0"
},
"jest": {
"setupFiles": [
"./tests/jest-setup.js"
],
"testPathIgnorePatterns": [
"<rootDir>/tests/kalendaryo.spec.js"
]
},
"config": {
Expand Down
4 changes: 4 additions & 0 deletions src/__tests__/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": ["../../.eslintrc.json", "plugin:jest/recommended"],
"plugins": ["jest"]
}
231 changes: 231 additions & 0 deletions src/__tests__/methods.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
import {render, cleanup} from 'react-testing-library'
import {
format,
addMonths,
subMonths,
setDate,
getDate,
startOfMonth,
startOfWeek,
addWeeks,
isSameWeek
} from 'date-fns'

beforeEach(() => {
expect.hasAssertions()
console.error = jest.fn()
})

afterEach(cleanup)

describe('Methods', () => {
describe('#getFormattedDate', () => {
test('Formats state.date as \'MM/DD/YY\' by default when no arguments are given', () => {
const {getFormattedDate} = setup().renderArgs
const today = new Date()
const defaultFormat = 'MM/DD/YY'
expect(getFormattedDate()).toBe(format(today, defaultFormat))
})

test('Properly formats a date from the given format string', () => {
const {date, getFormattedDate} = setup().renderArgs
const dateFormats = ['MM/DD/YYYY', 'MMMM Do, YYYY', 'MMM Mo']

dateFormats.forEach(df => {
expect(getFormattedDate(df)).toBe(format(date, df))
expect(getFormattedDate(date, df)).toBe(format(date, df))
})
})

test('Throws an error when passed arguments are invalid', () => {
const {date, getFormattedDate} = setup().renderArgs
const invalidArgs = [1, null, false, [], {}]
invalidArgs.forEach(arg => {
expect(() => getFormattedDate(arg)).toThrow()
expect(() => getFormattedDate(date, arg)).toThrow()
})
})
})

describe('#getDateNextMonth', () => {
test('Adds 1 month to state.date when no arguments are given', () => {
const {date, getFormattedDate, getDateNextMonth} = setup().renderArgs
const nextMonth = getFormattedDate(getDateNextMonth())
const actualNextMonth = getFormattedDate(addMonths(date, 1))
expect(nextMonth).toBe(actualNextMonth)
})

test('Properly adds a given month to a date', () => {
const {date, getFormattedDate, getDateNextMonth} = setup().renderArgs
const threshold = 3
let index = 0
while (index < threshold) {
const n = getRandomNum(10)
const month = getFormattedDate(getDateNextMonth(n))
const monthWithDate = getFormattedDate(getDateNextMonth(date, n))
const actualMonth = getFormattedDate(addMonths(date, n))
expect(month).toBe(actualMonth)
expect(monthWithDate).toBe(actualMonth)
index += 1
}
})

test('Throws an error when passed arguments are invalid', () => {
const {date, getDateNextMonth} = setup().renderArgs
const invalidArgs = ['', null, false, [], {}]
invalidArgs.forEach(arg => {
expect(() => getDateNextMonth(arg)).toThrow()
expect(() => getDateNextMonth(date, arg)).toThrow()
})
})
})

describe('#getDatePrevMonth', () => {
test('Subtracts 1 month to state.date when no arguments are given', () => {
const {date, getFormattedDate, getDatePrevMonth} = setup().renderArgs
const prevMonth = getFormattedDate(getDatePrevMonth())
const actualPrevMonth = getFormattedDate(subMonths(date, 1))
expect(prevMonth).toBe(actualPrevMonth)
})

test('Properly subtracts a given month to a date', () => {
const {date, getFormattedDate, getDatePrevMonth} = setup().renderArgs
const threshold = 3
let index = 0
while (index < threshold) {
const n = getRandomNum(10)
const month = getFormattedDate(getDatePrevMonth(n))
const monthWithDate = getFormattedDate(getDatePrevMonth(date, n))
const actualMonth = getFormattedDate(subMonths(date, n))
expect(month).toBe(actualMonth)
expect(monthWithDate).toBe(actualMonth)
index += 1
}
})

test('Throws an error when passed arguments are invalid', () => {
const {date, getDatePrevMonth} = setup().renderArgs
const invalidArgs = ['', null, false, [], {}]
invalidArgs.forEach(arg => {
expect(() => getDatePrevMonth(arg)).toThrow()
expect(() => getDatePrevMonth(date, arg)).toThrow()
})
})
})

describe('#getDaysInMonth', () => {
test('Returns an array of days in the month of state.date if no arguments are given', () => {
const {getFormattedDate, getDaysInMonth, date} = setup().renderArgs
getDaysInMonth().forEach(day => {
const {dateValue, label} = day
expect(label).toEqual(getDate(dateValue))
const theDay = getFormattedDate(dateValue)
const theActualDay = getFormattedDate(setDate(date, label))
expect(theDay).toBe(theActualDay)
})
})

test('Properly returns an array of days in the month of the given date', () => {
const {
getFormattedDate,
getDaysInMonth,
getDateNextMonth,
getDatePrevMonth
} = setup().renderArgs

const dice = getRandomNum(2)
const num = getRandomNum(2)
const date = (
dice === 2
? getDateNextMonth(num)
: getDatePrevMonth(num)
)

getDaysInMonth(date).forEach(day => {
const {dateValue, label} = day
const theDay = getFormattedDate(dateValue)
const theActualDay = getFormattedDate(setDate(date, label))
expect(theDay).toBe(theActualDay)
})
})

test('Throws an error if the passed argument is not a Date', () => {
const {getDaysInMonth} = setup().renderArgs
const invalidArgs = ['', 1, null, false, [], {}]
invalidArgs.forEach(arg => {
expect(() => getDaysInMonth(arg)).toThrow()
})
})
})

describe('#getWeeksInMonth', () => {
test('Returns an array of weeks for the month in state.date when no arguments are given', () => {
const {getWeeksInMonth, date} = setup().renderArgs
const firstDayOfMonth = startOfMonth(date)
let weekOfMonth = startOfWeek(firstDayOfMonth)
getWeeksInMonth().forEach(week => {
expect(week).toBeInstanceOf(Array)
week.forEach(day => {
const {dateValue} = day
expect(isSameWeek(dateValue, weekOfMonth)).toBe(true)
})
weekOfMonth = addWeeks(weekOfMonth, 1)
})
})

test('Properly returns an array with weeks starting at a certain day', () => {
const {getWeeksInMonth, date} = setup().renderArgs
const days = ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat']
const dayIndex = getRandomNum(days.length)
const actualDay = days[dayIndex]
getWeeksInMonth(date, dayIndex).forEach(week => {
const {dateValue} = week[0]
const day = format(dateValue, 'ddd').toLowerCase()
expect(day).toBe(actualDay)
})
})

test('Throws an error when arguments are invalid', () => {
const {getWeeksInMonth, date} = setup().renderArgs
const invalidArgs = ['', null, false, [], {}]
invalidArgs.forEach(arg => {
expect(() => getWeeksInMonth(arg)).toThrow()
expect(() => getWeeksInMonth(date, arg)).toThrow()
})
})
})

describe('#getDayLabelsInWeek', () => {
test('Returns the array of days on a week that starts on sunday by default', () => {
const {getDayLabelsInWeek} = setup().renderArgs
const days = ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat']
getDayLabelsInWeek().forEach((dayOfWeek, i) => {
const actualDayOfWeek = days[i]
expect(dayOfWeek.toLowerCase()).toBe(actualDayOfWeek)
})
})

test('Properly sets the starting day base on the given argument on props.startWeekAt', () => {
const days = ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat']
const startWeekAt = getRandomNum(days.length)
const actualFirstDayOfWeek = days[startWeekAt]
const {getDayLabelsInWeek} = setup({startWeekAt}).renderArgs
const firstDayOfWeek = getDayLabelsInWeek()[0].toLowerCase()
expect(firstDayOfWeek).toBe(actualFirstDayOfWeek)
})
})
})

function setup ({ render: layout = () => <div />, ...props } = {}) {
let renderArgs
const renderSpy = jest.fn(props => {
renderArgs = props
return layout()
})
const utils = render(<Kalendaryo {...props} render={renderSpy} />)
return {renderSpy, renderArgs, ...utils}
}

function getRandomNum (num) {
return Math.ceil(Math.random() * num)
}

0 comments on commit 38a9943

Please sign in to comment.