From 166bec603e1f3ebecf152db9c80ebdfb1f66d140 Mon Sep 17 00:00:00 2001 From: Andrew James <13269277+andrew-t-james@users.noreply.github.com> Date: Thu, 31 Mar 2022 20:41:18 -0500 Subject: [PATCH] Add fix tests without env vars and run app with env.local (#344) Co-authored-by: Andrew James --- .env.example => .env.local | 12 ++++++++++-- .husky/pre-commit | 4 ++++ mocks/handlers.js | 6 +++--- package-lock.json | 6 ++++++ package.json | 12 ++++++++++-- src/pages/api/subscribe.js | 15 +++++++++------ tests/api/subscribe.test.js | 3 +++ 7 files changed, 45 insertions(+), 13 deletions(-) rename .env.example => .env.local (52%) create mode 100755 .husky/pre-commit diff --git a/.env.example b/.env.local similarity index 52% rename from .env.example rename to .env.local index 91ff0554c..fe698bc73 100644 --- a/.env.example +++ b/.env.local @@ -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 +# 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= diff --git a/.husky/pre-commit b/.husky/pre-commit new file mode 100755 index 000000000..449fcdee1 --- /dev/null +++ b/.husky/pre-commit @@ -0,0 +1,4 @@ +#!/bin/sh +. "$(dirname "$0")/_/husky.sh" + +npm test diff --git a/mocks/handlers.js b/mocks/handlers.js index 5055208f4..9aa2395c1 100644 --- a/mocks/handlers.js +++ b/mocks/handlers.js @@ -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) => { diff --git a/package-lock.json b/package-lock.json index c1cf0af95..bccd5e82c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -19930,6 +19930,12 @@ "integrity": "sha512-B4FFZ6q/T2jhhksgkbEW3HBvWIfDW85snkQgawt07S7J5QXTk6BkNV+0yAeZrM5QpMAdYlocGoljn0sJ/WQkFw==", "dev": true }, + "husky": { + "version": "7.0.4", + "resolved": "https://registry.npmjs.org/husky/-/husky-7.0.4.tgz", + "integrity": "sha512-vbaCKN2QLtP/vD4yvs6iz6hBEo6wkSzs8HpRah1Z6aGmF2KW5PdYuAd7uX5a+OyBZHBhd+TFLqgjUgytQr4RvQ==", + "dev": true + }, "iconv-lite": { "version": "0.4.24", "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz", diff --git a/package.json b/package.json index 2d2d26df6..2d17c0614 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", @@ -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" + } } } diff --git a/src/pages/api/subscribe.js b/src/pages/api/subscribe.js index 079513758..cd62fe5a3 100644 --- a/src/pages/api/subscribe.js +++ b/src/pages/api/subscribe.js @@ -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']) @@ -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 => { diff --git a/tests/api/subscribe.test.js b/tests/api/subscribe.test.js index 32e888000..7a396336c 100644 --- a/tests/api/subscribe.test.js +++ b/tests/api/subscribe.test.js @@ -12,6 +12,9 @@ describe('subscribe handler', () => { method: 'POST', statusCode: 200, body: JSON.stringify(body), + headers: { + Accept: 'application/json', + }, }) await subscribeApiHandler(req, res)