Skip to content

Commit 550716d

Browse files
authored
Merge pull request #333 from EnCiv/civ-serv-listening#332
Civ serv listening#332
2 parents ea00a63 + 981fd86 commit 550716d

13 files changed

+53615
-14602
lines changed

.storybook/main.js

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
const webpack = require('webpack')
2+
3+
module.exports = {
4+
stories: ['../stories/**/*.stories.mdx', '../stories/**/*.stories.@(js|jsx|ts|tsx)'],
5+
addons: ['@storybook/addon-links', '@storybook/addon-essentials'],
6+
framework: '@storybook/react',
7+
core: {
8+
builder: 'webpack5',
9+
},
10+
webpackFinal: async config => {
11+
// config may or may not have these properties so we have to make sure they are there and then modify them to ensure we don't delete anything
12+
if (!config.resolve) config.resolve = {}
13+
if (!config.resolve.fallback) config.resolve.fallback = {}
14+
Object.assign(config.resolve.fallback, {
15+
fs: false,
16+
buffer: require.resolve('buffer'),
17+
path: require.resolve('path-browserify'),
18+
stream: require.resolve('stream-browserify'),
19+
os: require.resolve('os-browserify/browser'),
20+
zlib: require.resolve('browserify-zlib'),
21+
constants: require.resolve('constants-browserify'),
22+
util: require.resolve('util'),
23+
})
24+
if (!config.plugins) config.plugins = []
25+
config.plugins.push(new webpack.ProvidePlugin({ Buffer: ['buffer', 'Buffer'] }))
26+
config.plugins.push(new webpack.ProvidePlugin({ process: 'process/browser' })) // fix "process is not defined" error: // (do "npm install process" before running the build)
27+
for (const plugin of config.plugins) {
28+
if (plugin.definitions) {
29+
if (plugin.definitions['process.env']) {
30+
console.info(
31+
'.storybook/main.js: deleting process.env from',
32+
{ plugin },
33+
'see comments in that file'
34+
)
35+
delete plugin.definitions['process.env']
36+
/*
37+
webpack will try to string replace process.env with what is assigned in the definition.
38+
// But if there is code in the browser side that does something like "if(process.env)" it will get replaced and cause syntax error and break the existing code
39+
40+
definitions{
41+
...
42+
"process.env": "{\"NODE_ENV\":\"development\",\"NODE_PATH\":[],\"STORYBOOK\":\"true\",\"PUBLIC_URL\":\".\"}",
43+
...
44+
}
45+
46+
causes this:
47+
if (!process.env.NODE_ENV) process.env.NODE_ENV = 'development'
48+
to become
49+
if (!{"NODE_ENV":"development","NODE_PATH":["/usr/lib64/node_modules"],"STORYBOOK":"true","PUBLIC_URL":"."}) {"NODE_ENV":"development","NODE_PATH":["/usr/lib64/node_modules"],"STORYBOOK":"true","PUBLIC_URL":"."} = {};
50+
*/
51+
}
52+
}
53+
}
54+
return config
55+
},
56+
}

.storybook/preview.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import React from 'react'
2+
import { ThemeProvider } from 'react-jss'
3+
import theme from '../app/theme'
4+
5+
export const parameters = {
6+
actions: { argTypesRegex: '^on[A-Z].*' },
7+
}
8+
9+
export const decorators = [
10+
Story => {
11+
if (typeof window.logger === 'undefined') window.logger = console
12+
if (typeof socket === 'undefined') window.socket = {}
13+
window.socket.NoSocket = true
14+
return (
15+
<ThemeProvider theme={theme}>
16+
<div style={{ backgroundColor: theme.backgroundColorApp }}>
17+
<Story />
18+
</div>
19+
</ThemeProvider>
20+
)
21+
},
22+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
import setOrDeleteWithMessages from '../set-or-delete-with-messages'
2+
3+
test('change the value of a property', () => {
4+
const dst = { a: 1 }
5+
const src = { a: 2 }
6+
let messages = []
7+
setOrDeleteWithMessages(dst, src, messages)
8+
expect(messages).toMatchInlineSnapshot(`
9+
Array [
10+
"a: changing 1 to 2",
11+
]
12+
`)
13+
})
14+
15+
test('change the value of a property and leave other property alone', () => {
16+
const dst = { a: 1, b: 2 }
17+
const src = { a: 2 }
18+
let messages = []
19+
setOrDeleteWithMessages(dst, src, messages)
20+
expect(dst).toMatchObject(src)
21+
expect(messages).toMatchInlineSnapshot(`
22+
Array [
23+
"a: changing 1 to 2",
24+
]
25+
`)
26+
})
27+
28+
test('adding an obj', () => {
29+
const dst = { a: 1, b: 2 }
30+
const src = { c: { d: 4, e: 5 } }
31+
let messages = []
32+
setOrDeleteWithMessages(dst, src, messages)
33+
expect(dst).toMatchObject(src)
34+
expect(messages).toMatchInlineSnapshot(`
35+
Array [
36+
"c: changing not-present to {
37+
\\"d\\": 4,
38+
\\"e\\": 5
39+
}",
40+
]
41+
`)
42+
})
43+
44+
test('deleting a property', () => {
45+
const dst = { a: 1, b: 2 }
46+
const src = { c: 3, a: undefined }
47+
let messages = []
48+
setOrDeleteWithMessages(dst, src, messages)
49+
expect(dst).toMatchObject({ b: 2, c: 3 })
50+
expect(messages).toMatchInlineSnapshot(`
51+
Array [
52+
"c: changing not-present to 3",
53+
"deleting a: 1",
54+
]
55+
`)
56+
})
57+
58+
test('make an array smaller', () => {
59+
const dst = [1, 2, 3]
60+
const src = [1, 2]
61+
let messages = []
62+
setOrDeleteWithMessages(dst, src, messages)
63+
expect(dst).toMatchObject(src)
64+
expect(messages).toMatchInlineSnapshot(`
65+
Array [
66+
"deleting [2]: 3",
67+
]
68+
`)
69+
})
70+
71+
test('make an array within an object smaller', () => {
72+
const dst = { a: [1, 2, 3], b: 1, c: { a: 1 } }
73+
const src = { a: [1, 2] }
74+
let messages = []
75+
setOrDeleteWithMessages(dst, src, messages)
76+
expect(dst).toMatchObject({ a: [1, 2], b: 1, c: { a: 1 } })
77+
expect(messages).toMatchInlineSnapshot(`
78+
Array [
79+
"deleting a[2]: 3",
80+
]
81+
`)
82+
})
83+
84+
test('can not set an object to a string', () => {
85+
const dst = { a: 1 }
86+
const src = "can't set"
87+
let messages = []
88+
setOrDeleteWithMessages(dst, src, messages)
89+
expect(dst).toMatchObject(dst)
90+
expect(messages).toMatchInlineSnapshot(`
91+
Array [
92+
"Can not change type object to string",
93+
]
94+
`)
95+
})
96+
97+
test('can set a deeper object to a string', () => {
98+
const dst = { a: 1, b: { c: 2 } }
99+
const src = { b: 'string' }
100+
let messages = []
101+
setOrDeleteWithMessages(dst, src, messages)
102+
expect(dst).toMatchObject({ a: 1, b: 'string' })
103+
expect(messages).toMatchInlineSnapshot(`
104+
Array [
105+
"b: changing {
106+
\\"c\\": 2
107+
} to \\"string\\"",
108+
]
109+
`)
110+
})
111+
112+
function be() {}
113+
function func() {}
114+
function b1() {
115+
return 1
116+
}
117+
function newFunc() {
118+
return 1
119+
}
120+
121+
test('can work with functions', () => {
122+
const dst = { a: 1, b: be, c: { func: func } }
123+
const src = { a: 'string', b: b1, c: { newFunc: newFunc } }
124+
let messages = []
125+
setOrDeleteWithMessages(dst, src, messages)
126+
expect(dst).toMatchObject({ a: 'string', b: b1, c: { func: func, newFunc: newFunc } })
127+
expect(messages).toMatchInlineSnapshot(`
128+
Array [
129+
"a: changing 1 to \\"string\\"",
130+
"b: changing be() to b1()",
131+
"c.newFunc: changing not-present to newFunc()",
132+
]
133+
`)
134+
})
135+
136+
test('can work without messages', () => {
137+
const dst = { a: 1, b: be, c: { func: func } }
138+
const src = { a: 'string', b: b1, c: { newFunc: newFunc } }
139+
setOrDeleteWithMessages(dst, src)
140+
expect(dst).toMatchObject({ a: 'string', b: b1, c: { func: func, newFunc: newFunc } })
141+
})
142+
143+
test('can sort of change an obj to an array', () => {
144+
const dst = { 0: 'a', 1: 'b', 2: 'c' }
145+
const src = ['d', 'e', 'f']
146+
let messages = []
147+
setOrDeleteWithMessages(dst, src, messages)
148+
expect(dst).toMatchObject({ 0: 'd', 1: 'e', 2: 'f' })
149+
expect(messages).toMatchInlineSnapshot(`
150+
Array [
151+
"0: changing \\"a\\" to \\"d\\"",
152+
"1: changing \\"b\\" to \\"e\\"",
153+
"2: changing \\"c\\" to \\"f\\"",
154+
]
155+
`)
156+
})
157+
158+
test('can change false to true and numbers to strings and vice versa', () => {
159+
const dst = { a: true, b: false, c: 3, d: 'four' }
160+
const src = { a: false, b: true, c: 'three', d: 4 }
161+
let messages = []
162+
setOrDeleteWithMessages(dst, src, messages)
163+
expect(dst).toMatchObject({ a: false, b: true, c: 'three', d: 4 })
164+
expect(messages).toMatchInlineSnapshot(`
165+
Array [
166+
"a: changing true to false",
167+
"b: changing false to true",
168+
"c: changing 3 to \\"three\\"",
169+
"d: changing \\"four\\" to 4",
170+
]
171+
`)
172+
})
173+
174+
test('make changes to sparse arrays', () => {
175+
const dst = { a: ['a', 'b', 'c'], b: 6 }
176+
dst.a[7] = 'z'
177+
const src = { a: ['a', 'b', 'c'] }
178+
src.a[7] = 'zz'
179+
src.a[6] = 'six'
180+
const result = { a: ['a', 'b', 'c'], b: 6 }
181+
result.a[6] = 'six'
182+
result.a[7] = 'zz'
183+
let messages = []
184+
setOrDeleteWithMessages(dst, src, messages)
185+
expect(dst).toMatchObject(result)
186+
expect(messages).toMatchInlineSnapshot(`
187+
Array [
188+
"a[6]: changing not-present to \\"six\\"",
189+
"a[7]: changing \\"z\\" to \\"zz\\"",
190+
]
191+
`)
192+
})
193+
194+
test('add a deep deep prop', () => {
195+
const dst = { a: 1 }
196+
const src = { b: { c: { d: { d: 2 } } } }
197+
let messages = []
198+
setOrDeleteWithMessages(dst, src, messages)
199+
expect(dst).toMatchObject({ a: 1, b: { c: { d: { d: 2 } } } })
200+
expect(messages).toMatchInlineSnapshot(`
201+
Array [
202+
"b: changing not-present to {
203+
\\"c\\": {
204+
\\"d\\": {
205+
\\"d\\": 2
206+
}
207+
}
208+
}",
209+
]
210+
`)
211+
})

0 commit comments

Comments
 (0)