Skip to content

Commit

Permalink
👷 ✅ Add tests and config CI to run that
Browse files Browse the repository at this point in the history
  • Loading branch information
YuneVK committed Apr 11, 2022
1 parent 71474ff commit 91af810
Show file tree
Hide file tree
Showing 8 changed files with 3,249 additions and 47 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ jobs:
- name: Install and Build 🔧 # This example project is built using npm and outputs the result to the 'build' folder. Replace with the commands required to build your project, or remove this step entirely if your site is pre-built.
run: |
npm install
npm run test
npm run build
- name: Add .nojekyll file
Expand Down
23 changes: 23 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: build-and-deploy
on:
pull_request:
branches:
- main

jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout 🛎️
uses: actions/checkout@v3

- name: Install, Test, and Build 🔧
uses: actions/setup-node@v2
with:
node-version: '14'

- name: Install, Test, and Build 🔧
run: |
npm install
npm run test
npm run build
32 changes: 32 additions & 0 deletions api/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import utc from 'dayjs/plugin/utc'
import timezone from 'dayjs/plugin/timezone'

import { getDataFromMD } from '../common/api'
import TYPES from '../components/event-type-chip/types'

dayjs.extend(utc)
dayjs.extend(timezone)
Expand Down Expand Up @@ -39,6 +40,18 @@ export const getEventBySlug = (slug) => {
export const parseEvents = (events) => events.map(parseEvent)

export const parseEvent = (event) => {
if (!event.title) {
throw new TypeError('Event must have a title.')
}

if (Object.keys(TYPES).indexOf(event.type) === -1) {
throw new TypeError(
`Event must be one of the following types: ${Object.keys(TYPES).join(
', ',
)}`,
)
}

const formattedDate = formatEventDateTime(
event.date,
event.UTCStartTime,
Expand All @@ -55,6 +68,11 @@ export const parseEvent = (event) => {
const formatEventDateTime = (date = dayjs.utc(), startTime, endTime) => {
// Date
const [month, day] = date.split('/')

if (isNaN(month) || isNaN(day)) {
throw new TypeError('date must be in mm/dd format (e.g. 06/12).')
}

const UTCDate = dayjs
.utc()
.date(day)
Expand All @@ -68,6 +86,13 @@ const formatEventDateTime = (date = dayjs.utc(), startTime, endTime) => {
const [startHour, startMinute] = startTime.split(':')

const UTCStartTime = UTCDate.hour(startHour).minute(startMinute)

if (isNaN(UTCStartTime)) {
throw new TypeError(
'UTCStartTime must be in hh:mm format (e.g. 12:30) or be "TDB".',
)
}

const PTStartTime = UTCStartTime.tz('America/Los_Angeles')

const formattedUTCStartTime = UTCStartTime.format('HH:mm a')
Expand All @@ -86,6 +111,13 @@ const formatEventDateTime = (date = dayjs.utc(), startTime, endTime) => {
const [endHour, endMinute] = endTime.split(':')

const UTCEndTime = UTCDate.hour(endHour).minute(endMinute)

if (isNaN(UTCStartTime)) {
throw new TypeError(
'UTCEndTime must be in hh:mm format (e.g. 12:30) or be "TDB".',
)
}

const PTEndTime = UTCEndTime.tz('America/Los_Angeles')

const formattedUTCEndTime = UTCEndTime.format('HH:mm a')
Expand Down
11 changes: 11 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// jest.config.js
const nextJest = require('next/jest')

const createJestConfig = nextJest({ dir: './' })

const customJestConfig = {
moduleDirectories: ['node_modules', '<rootDir>/'],
testEnvironment: 'jest-environment-jsdom',
}

module.exports = createJestConfig(customJestConfig)
9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"dev": "next dev",
"build": "NODE_ENV=production next build && next export",
"start": "next start",
"lint": "next lint"
"lint": "next lint",
"test": "jest"
},
"dependencies": {
"clsx": "^1.1.1",
Expand All @@ -19,8 +20,14 @@
"react-dom": "17.0.2"
},
"devDependencies": {
"@babel/preset-env": "^7.16.11",
"@testing-library/jest-dom": "^5.16.4",
"@testing-library/react": "^13.0.0",
"babel-jest": "^27.5.1",
"eslint": "8.11.0",
"eslint-config-next": "12.1.0",
"jest": "^27.5.1",
"jest-css-modules": "^2.1.0",
"sass": "^1.49.9",
"sass-loader": "^12.6.0"
}
Expand Down
27 changes: 27 additions & 0 deletions tests/events.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import fs from 'fs'

import { getEventBySlug, parseEvent } from '../api/events'

describe('Event files', () => {
test('All event files have ".md" extension', () => {
const eventFiles = fs.readdirSync('content/events')

const areAllValid = eventFiles.every((fileName) => fileName.endsWith('.md'))

expect(areAllValid).toBe(true)
})

test('All events have correct dates (or TBD)', () => {
const eventFiles = fs.readdirSync('content/events')

const events = eventFiles.map((fileName) => {
const slug = fileName.replace('.md', '')
const event = getEventBySlug(slug)
return event
})

events.forEach((event) => {
expect(() => parseEvent(event)).not.toThrowError()
})
})
})
30 changes: 30 additions & 0 deletions tests/resources.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import resourcesJSON from '../content/library/resources.json'

const { resources } = resourcesJSON

describe('Resources', () => {
test('Resources must have a title', () => {
const areAllValid = resources.every((resource) => resource.title)
expect(areAllValid).toBe(true)
})

test('Resources must have an author', () => {
const areAllValid = resources.every((resource) => resource.author)
expect(areAllValid).toBe(true)
})

test('Resources must have a link', () => {
const areAllValid = resources.every((resource) => resource.link)
expect(areAllValid).toBe(true)
})

test('Resources must have a type', () => {
const areAllValid = resources.every((resource) => resource.type)
expect(areAllValid).toBe(true)
})

test('Resources must have topics', () => {
const areAllValid = resources.every((resource) => resource.topics)
expect(areAllValid).toBe(true)
})
})
Loading

0 comments on commit 91af810

Please sign in to comment.