Skip to content

Commit 77cd5f0

Browse files
committed
Added test harness, added tests for processKnob() and fixed it.
1 parent e99d45a commit 77cd5f0

File tree

6 files changed

+76
-35
lines changed

6 files changed

+76
-35
lines changed

.eslintrc.yml

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ plugins:
66
env:
77
es6: true
88
browser: true
9+
jest: true
910
globals:
1011
BACKGROUNDS: true
1112
ICONS: true

components/00-base/base.utils.js

+17-32
Original file line numberDiff line numberDiff line change
@@ -488,47 +488,32 @@ export class KnobValues {
488488

489489
/**
490490
* Process values passed to the knob and return a value or render a knob.
491-
*
492-
* Do not optimize this function - it is laid out in a way that is easy to
493-
* understand and follow the logic.
494491
*/
495-
const processKnob = (name, defaultValue, parent, group, knobCallback) => {
496-
if (parent) {
497-
// If parent was passed, and it is not a KnobValue instance, then it
498-
// represents a directly passed value that should be used without
499-
// rendering the knob.
500-
if (!(parent instanceof KnobValue)) {
501-
return parent;
502-
}
503-
504-
// If parent was passed, and it is KnobValue instance set to use the default
505-
// value, then use the default value passed to the knob without rendering
506-
// the knob itself.
507-
if (parent.isUsingDefault()) {
508-
return defaultValue;
509-
}
492+
export const processKnob = (name, defaultValue, parent, group, knobCallback) => {
493+
// If parent is undefined, use the default value and render the knob.
494+
if (parent === undefined) {
495+
return knobCallback(name, defaultValue, group);
510496
}
511497

512-
if (parent === null || parent === false) {
498+
// If parent is null, a scalar value or an object, use it's value.
499+
if (parent === null || !(parent instanceof KnobValue)) {
513500
return parent;
514501
}
515502

516-
let val;
503+
// If parent is a KnobValue instance set to use the default value, return the
504+
// default value.
505+
if (parent && parent.isUsingDefault()) {
506+
return defaultValue;
507+
}
517508

518-
if (parent === undefined) {
519-
// If parent was not provided - use the default value in the knob.
520-
val = defaultValue;
521-
} else if (parent.getValue() === null) {
522-
// If parent was provided as a KnobValue with no value - use the default
523-
// value in the knob.
524-
val = defaultValue;
525-
} else {
526-
// If parent was provided as a KnobValue with a value - use this value in
527-
// the knob.
528-
val = parent.getValue();
509+
// If parent is a KnobValue instance with a null value, use the default value
510+
// and render the knob.
511+
if (parent.getValue() === null) {
512+
return knobCallback(name, defaultValue, group);
529513
}
530514

531-
return knobCallback(name, val, group);
515+
// Use the value from the KnobValue instance.
516+
return knobCallback(name, parent.getValue(), group);
532517
};
533518

534519
export const knobText = (name, value, parent, group = 'General') => processKnob(name, value, parent, group, (knobName, knobValue, knobGroup) => text(knobName, knobValue, knobGroup));

components/00-base/base.utils.test.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { KnobValue, processKnob } from './base.utils';
2+
3+
// eslint-disable-next-line no-unused-vars
4+
const knobCallback = jest.fn((name, value, group) => value);
5+
6+
const dataProviderProcessKnob = () => [
7+
['name', 'default', undefined, 'group', knobCallback, 'default'],
8+
['name', 'default', null, 'group', knobCallback, null],
9+
['name', 'default', false, 'group', knobCallback, false],
10+
['name', 'default', true, 'group', knobCallback, true],
11+
12+
['name', 'default', 'direct value', 'group', knobCallback, 'direct value'],
13+
['name', 'default', { a: 'b' }, 'group', knobCallback, { a: 'b' }],
14+
15+
['name', 'default', new KnobValue(null), 'group', knobCallback, 'default'],
16+
['name', 'default', new KnobValue(null, true), 'group', knobCallback, 'default'],
17+
['name', 'default', new KnobValue(null, false), 'group', knobCallback, 'default'],
18+
19+
['name', 'default', new KnobValue('value'), 'group', knobCallback, 'value'],
20+
['name', 'default', new KnobValue('value', true), 'group', knobCallback, 'default'],
21+
['name', 'default', new KnobValue('value', false), 'group', knobCallback, 'value'],
22+
];
23+
24+
describe.each(dataProviderProcessKnob())(
25+
'processKnob(%s, %s, %p, %s, knobCallback)',
26+
(name, defaultValue, parent, group, cb, expected) => {
27+
test(`returns ${expected}`, () => {
28+
if (typeof expected === 'object') {
29+
expect(processKnob(name, defaultValue, parent, group, cb)).toStrictEqual(expected);
30+
} else {
31+
expect(processKnob(name, defaultValue, parent, group, cb)).toBe(expected);
32+
}
33+
});
34+
},
35+
);

jest.config.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module.exports = {
2+
transform: {
3+
'^.+\\.jsx?$': ['babel-jest', {
4+
presets: [
5+
['@babel/preset-env', { targets: { node: 'current' } }],
6+
],
7+
}],
8+
},
9+
testEnvironment: 'node',
10+
moduleNameMapper: {
11+
'\\.(jpg|jpeg|png|svg|ico|woff|woff2|ttf|eot|webm|avi|mp4|twig)$': 'jest-transform-stub',
12+
},
13+
transformIgnorePatterns: [
14+
'node_modules/(?!(@storybook/addon-knobs)/)',
15+
],
16+
};

package.json

+6-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
"lint-fix": "eslint ./components ./.storybook ./webpack --fix && stylelint 'components/**/*.scss' --fix",
2626
"dist": "export NODE_OPTIONS=--openssl-legacy-provider && webpack --config ./webpack/webpack.prod.js",
2727
"dist-dev": "export NODE_OPTIONS=--openssl-legacy-provider && webpack --config ./webpack/webpack.dev.js",
28-
"test": "npm run test-storybook:ci",
28+
"test": "npm run test-unit && npm run test-storybook:ci",
29+
"test-unit": "jest",
2930
"test-storybook": "test-storybook",
3031
"test-storybook:ci": "concurrently --kill-others --success first --names \"SB,TEST\" --prefix-colors \"magenta,blue\" \"npx http-server storybook-static --port 6006 -a 127.0.0.1 --silent\" \"wait-on --timeout 10000 http://127.0.0.1:6006 && npm run test-storybook\"",
3132
"reset": "rm -rf node_modules; rm -rf package-lock.json"
@@ -42,7 +43,8 @@
4243
"@a110/storybook-expand-all": "^1.1.1",
4344
"@alexskrypnyk/scss-variables-extractor": "^0.1.0",
4445
"@babel/cli": "^7.15.4",
45-
"@babel/core": "^7.15.5",
46+
"@babel/core": "^7.24.7",
47+
"@babel/preset-env": "^7.24.7",
4648
"@playwright/test": "^1.40.1",
4749
"@storybook/addon-a11y": "^6.1.18",
4850
"@storybook/addon-essentials": "^6.3.8",
@@ -52,6 +54,7 @@
5254
"@storybook/html": "^6.3.8",
5355
"@storybook/test-runner": "^0.16.0",
5456
"addon-screen-reader": "^1.8.6",
57+
"babel-jest": "^29.7.0",
5558
"babel-loader": "^8.2.2",
5659
"babel-plugin-syntax-dynamic-import": "^6.18.0",
5760
"babel-plugin-transform-strict-mode": "^6.24.1",
@@ -70,6 +73,7 @@
7073
"glob-gitignore": "^1.0.14",
7174
"html-webpack-plugin": "^4.5.2",
7275
"http-server": "^14.1.1",
76+
"jest-transform-stub": "^2.0.0",
7377
"lorem-ipsum": "^2.0.4",
7478
"mini-css-extract-plugin": "^1.6.2",
7579
"node-sass-magic-importer": "^5.3.2",

webpack/webpack.common.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ module.exports = {
116116
},
117117
// Wrap JS with DOMContentLoaded.
118118
{
119-
test: /components\/[^/]+\/(?!.*\.(stories|component|utils)\.js$).*\.js$/,
119+
test: /components\/[^/]+\/(?!.*\.(stories|component|utils|test)\.js$).*\.js$/,
120120
exclude: /(node_modules|webpack|themejs\.js|css\.js)/,
121121
use: [{
122122
loader: 'babel-loader',

0 commit comments

Comments
 (0)