Skip to content
This repository was archived by the owner on Mar 30, 2022. It is now read-only.

Commit 623d69c

Browse files
committed
in the middle of changing env var
1 parent 4e58540 commit 623d69c

File tree

17 files changed

+206
-78
lines changed

17 files changed

+206
-78
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ coverage
55
public/build/*
66
*.log
77
.env
8+
.env.*
9+
.env*.local
810
out/
911
public/workbox-*.js
1012
public/sw.js
1113
public/work-*.js
1214
public/manifest.json
15+
env

configs/env.mapping.js

-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
/* eslint-disable @typescript-eslint/no-var-requires */
2-
const path = require('path');
3-
require('dotenv').config({
4-
path: path.join(__dirname, `../.env/${process.env.NODE_ENV}.env`),
5-
});
62
const { dependencies } = require('../package-lock.json');
73

84
const FOLDER = process.env.FOLDER || '';

jest/jest.setup.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import Enzyme from 'enzyme';
22
import Adapter from 'enzyme-adapter-react-16';
3-
import envMapping from '../configs/env.mapping';
3+
import { join } from 'path';
4+
import { loadEnvConfig } from 'next/dist/lib/load-env-config';
45

5-
Object.assign(process.env, envMapping);
6+
loadEnvConfig(join(__dirname, './../'));
67

78
Enzyme.configure({ adapter: new Adapter() });

next.config.js

+11-10
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@ const withManifest = require('next-manifest');
44
const withTM = require('next-transpile-modules')(['typeorm/browser']);
55
const webpack = require('webpack');
66
const defaultCache = require('next-pwa/cache');
7-
const envMapping = require('./configs/env.mapping');
7+
const { dependencies } = require('./package-lock.json');
88

99
const isProd = process.env.NODE_ENV === 'production';
10-
const {
11-
LINK_PREFIX,
12-
FOLDER,
13-
THEME_COLOR,
14-
ICON_192_PATH,
15-
ICON_512_PATH,
16-
} = envMapping;
10+
const LINK_PREFIX = process.env.NEXT_PUBLIC_LINK_PREFIX || '';
11+
const FOLDER = LINK_PREFIX && LINK_PREFIX.substring(1);
12+
const THEME_COLOR = process.env.NEXT_PUBLIC_THEME_COLOR;
13+
const ICON_192_PATH = process.env.NEXT_PUBLIC_ICON_192_PATH;
14+
const ICON_512_PATH = process.env.NEXT_PUBLIC_ICON_512_PATH;
15+
const { SHORT_NAME } = process.env;
1716

1817
// tranfrom precache url for browsers that encode dynamic routes
1918
// i.e. "[id].js" => "%5Bid%5D.js"
@@ -46,7 +45,9 @@ module.exports = () =>
4645
return config;
4746
},
4847

49-
env: envMapping,
48+
env: {
49+
NEXT_PUBLIC_SQL_JS_VERSION: dependencies['sql.js'].version || '',
50+
},
5051
target: 'serverless',
5152
poweredByHeader: false,
5253
assetPrefix: LINK_PREFIX,
@@ -98,7 +99,7 @@ module.exports = () =>
9899
manifest: {
99100
/* eslint-disable @typescript-eslint/camelcase */
100101
output: 'public',
101-
short_name: FOLDER,
102+
short_name: SHORT_NAME || FOLDER,
102103
name: FOLDER,
103104
start_url: `${LINK_PREFIX}/`,
104105
background_color: THEME_COLOR,

package-lock.json

+48-27
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@
2727
"dependencies": {
2828
"@emotion/core": "^10.0.28",
2929
"@reduxjs/toolkit": "^1.3.6",
30-
"dotenv": "^8.2.0",
3130
"localforage": "^1.7.3",
32-
"next": "^9.4.0",
31+
"next": "^9.4.1",
3332
"next-manifest": "^3.0.1",
3433
"next-pwa": "^2.4.1",
3534
"next-transpile-modules": "^3.3.0",
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,90 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`IndexPage should render without throwing an error 1`] = `
4-
<IndexPage
5-
test="test"
4+
<Provider
5+
store={
6+
Object {
7+
"addReducer": [Function],
8+
"dispatch": [Function],
9+
"getState": [Function],
10+
"injectReducers": [Function],
11+
"reducer": Object {
12+
"count": [Function],
13+
},
14+
"removeReducers": [Function],
15+
"replaceReducer": [Function],
16+
"subscribe": [Function],
17+
"substituteReducers": [Function],
18+
Symbol(observable): [Function],
19+
}
20+
}
621
>
7-
<div>
8-
<p>
9-
Props from _app.tsx: {"test":"test"}
10-
</p>
11-
<p>
12-
page process env: TEST_TEST_PAGE_PROP
13-
</p>
14-
</div>
15-
</IndexPage>
22+
<IndexPage
23+
test="test"
24+
>
25+
<main>
26+
<ManifestHead
27+
hrefCanonical="/"
28+
title="github_pwa static-path"
29+
>
30+
<Head>
31+
<Component
32+
handleStateChange={null}
33+
inAmpMode={false}
34+
reduceComponentsToState={[Function]}
35+
/>
36+
</Head>
37+
</ManifestHead>
38+
<p>
39+
Props from _app.tsx: {"test":"test"}
40+
</p>
41+
<p>
42+
page process env: TEST_TEST_PAGE_PROP
43+
</p>
44+
<CountDivWrap>
45+
<DynamicStoreWrap
46+
callbackOnMount={[Function]}
47+
callbackOnUnmount={[Function]}
48+
>
49+
<DynamicStore
50+
callbackOnMount={[Function]}
51+
callbackOnUnmount={[Function]}
52+
store={
53+
Object {
54+
"addReducer": [Function],
55+
"dispatch": [Function],
56+
"getState": [Function],
57+
"injectReducers": [Function],
58+
"reducer": Object {
59+
"count": [Function],
60+
},
61+
"removeReducers": [Function],
62+
"replaceReducer": [Function],
63+
"subscribe": [Function],
64+
"substituteReducers": [Function],
65+
Symbol(observable): [Function],
66+
}
67+
}
68+
/>
69+
</DynamicStoreWrap>
70+
</CountDivWrap>
71+
<PrefixedLink
72+
href="/index/1"
73+
>
74+
<Link
75+
as="/index/1"
76+
href="/index/1"
77+
>
78+
<a
79+
href="/index/1"
80+
onClick={[Function]}
81+
onMouseEnter={[Function]}
82+
>
83+
index/1
84+
</a>
85+
</Link>
86+
</PrefixedLink>
87+
</main>
88+
</IndexPage>
89+
</Provider>
1690
`;

src/__tests__/pages/index.test.tsx

+33-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,42 @@
1-
import React from 'react';
1+
/* eslint-disable react/jsx-props-no-spreading */
2+
import React, { ReactChild } from 'react';
23
import { mount, ReactWrapper } from 'enzyme';
4+
import { Provider } from 'react-redux';
5+
import configureStore, {
6+
ModifiedConfigureStoreOptions,
7+
} from 'utils/redux/configureStore';
38
import IndexPage from 'pages/index';
9+
import { act } from 'react-dom/test-utils';
10+
11+
const mountWithStore = (children: ReactChild) => ({
12+
reducer = {},
13+
devTools = false,
14+
middleware,
15+
preloadedState,
16+
enhancers,
17+
}: ModifiedConfigureStoreOptions = {}) => {
18+
const store = configureStore({
19+
reducer,
20+
devTools,
21+
middleware,
22+
preloadedState,
23+
enhancers,
24+
});
25+
return mount(<Provider store={store}>{children}</Provider>);
26+
};
427

528
describe('IndexPage', () => {
629
const testProp = {
730
test: 'test',
831
};
9-
/* eslint-disable-next-line react/jsx-props-no-spreading */
10-
const TestIndexPageWrapper: ReactWrapper = mount(<IndexPage {...testProp} />);
32+
33+
let TestIndexPageWrapper: ReactWrapper;
34+
35+
beforeAll(async () => {
36+
await act(async () => {
37+
TestIndexPageWrapper = mountWithStore(<IndexPage {...testProp} />)();
38+
});
39+
});
1140

1241
it('should render without throwing an error', () => {
1342
expect(TestIndexPageWrapper).toMatchSnapshot();
@@ -21,7 +50,7 @@ describe('IndexPage', () => {
2150

2251
it('should render page process env correctly', () => {
2352
expect(TestIndexPageWrapper.find('p').at(1).text()).toContain(
24-
process.env.TEST_PAGE_VAR
53+
process.env.NEXT_PUBLIC_TEST_PAGE_VAR
2554
);
2655
});
2756
});

0 commit comments

Comments
 (0)