Skip to content

Commit

Permalink
Add fix tests without env vars and run app with env.local (#344)
Browse files Browse the repository at this point in the history
Co-authored-by: Andrew James <[email protected]>
  • Loading branch information
andrew-t-james and Andrew James authored Apr 1, 2022
1 parent d68387b commit 166bec6
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 13 deletions.
12 changes: 10 additions & 2 deletions .env.example → .env.local
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
# VWC_ACTIVE_ENV is one of local or remote.
# when VWC_ACTIVE_ENV is set to local all external data is
# sourced from a local json file
# <remote> will require all keys to be added to this file.
VWC_ACTIVE_ENV=local

# Contentful keys
CONTENTFUL_SPACE_ID=
CONTENTFUL_ACCESS_TOKEN=
# Optional flag to enable different environments available in Contentful local | master

# Optional flag to enable different environments available in Contentful develop | master
# Defaults to master environment
# When VWC_ACTIVE_ENV is set to local NO data is sourced form Contentful
CONTENTFUL_ENVIRONMENT=master

# api keys
# API keys for /src/pages/api
MAILCHIMP_SUBSCRIBE_URL=
MAILCHIMP_API_KEY=
MAILCHIMP_LIST_ID=
Expand Down
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npm test
6 changes: 3 additions & 3 deletions mocks/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import { rest } from 'msw'
* @export handlers[] list of handlers
*/
export const handlers = [
// mock slack reuqests
rest.post('https://hooks.slack.com/services/*', (req, res, ctx) => {
// mock slack requests
rest.post('https://hooks.slack.com/services/*', (_, res, ctx) => {
return res(ctx.status(200), ctx.json({ ok: true }))
}),
rest.post('https://us4.api.mailchimp.com/3.0/lists/*', (req, res, ctx) => {
rest.post('https://us4.api.mailchimp.com/3.0/lists/*/*', (_, res, ctx) => {
return res(ctx.status(200), ctx.json({ ok: true }))
}),
rest.get('https://production.shippingapis.com/*', (req, res, ctx) => {
Expand Down
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 10 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@
"start": "next start",
"export": "next export",
"storybook": "start-storybook -p 6006 -s ./public",
"build-storybook": "build-storybook -s public"
"build-storybook": "build-storybook -s public",
"prepare": "husky install"
},
"devDependencies": {
"@babel/core": "7.16.0",
Expand Down Expand Up @@ -80,6 +81,7 @@
"eslint-plugin-react": "7.27.1",
"eslint-plugin-react-hooks": "4.3.0",
"eslint-plugin-storybook": "0.5.7",
"husky": "^7.0.4",
"inquirer": "8.2.0",
"jest": "27.5.1",
"lint-staged": "12.1.2",
Expand All @@ -92,6 +94,12 @@
"regenerator-runtime": "0.13.9"
},
"lint-staged": {
"*": "yarn format"
"*.js": ["eslint --fix", "git add ."]
},
"husky": {
"hooks": {
"pre-commit": "lint-staged",
"pre-push": "lint-staged && npm test"
}
}
}
15 changes: 9 additions & 6 deletions src/pages/api/subscribe.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import axios from 'axios'
import { checkParams } from './api-helpers'

const isTest = process.env.NODE_ENV === 'test'
const MAILCHIMP_SUBSCRIBE_URL = 'https://us4.api.mailchimp.com/3.0/lists'
const baseURL = MAILCHIMP_SUBSCRIBE_URL
const MAILCHIMP_LIST_ID = isTest ? 'mock-list-id-1111' : process.env.MAILCHIMP_LIST_ID
const MAILCHIMP_API_KEY = isTest ? 'mock-api-key-2222' : process.env.MAILCHIMP_API_KEY
const AUTHORIZATION = `Basic ${Buffer.from(`anystring:${MAILCHIMP_API_KEY}`).toString('base64')}`

export default async function handler(req, res) {
const parsedBody = JSON.parse(req.body)
const hasErrors = checkParams(parsedBody, ['email'])
Expand All @@ -17,18 +24,14 @@ export default async function handler(req, res) {
status: 'subscribed',
}

const baseURL = `${process.env.MAILCHIMP_SUBSCRIBE_URL}`

try {
await axios({
method: 'POST',
baseURL,
url: `${process.env.MAILCHIMP_LIST_ID}/members`,
url: `/${MAILCHIMP_LIST_ID}/members`,
headers: {
Accept: 'application/json',
Authorization: `Basic ${Buffer.from(`anystring:${process.env.MAILCHIMP_API_KEY}`).toString(
'base64'
)}`,
Authorization: AUTHORIZATION,
},
data: payload,
}).catch(err => {
Expand Down
3 changes: 3 additions & 0 deletions tests/api/subscribe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ describe('subscribe handler', () => {
method: 'POST',
statusCode: 200,
body: JSON.stringify(body),
headers: {
Accept: 'application/json',
},
})

await subscribeApiHandler(req, res)
Expand Down

1 comment on commit 166bec6

@vercel
Copy link

@vercel vercel bot commented on 166bec6 Apr 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.