Skip to content

Commit 2fb75f7

Browse files
committed
Pass story parameters to fileNameFormatter
1 parent e0dcf22 commit 2fb75f7

11 files changed

+95
-57
lines changed

docs/configuration.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ Since JSON doesn't support functions, you need to be using the `loki.config.js`
105105
```js
106106
module.exports = {
107107
// ...other config goes here
108-
fileNameFormatter: ({ configurationName, kind, story }) =>
108+
fileNameFormatter: ({ configurationName, kind, story, parameters }) =>
109109
`${configurationName}/${kind} ${story}`.toLowerCase(),
110110
};
111111
```

examples/react/src/stories/Decorators.stories.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,5 @@ export const CustomChromeSelector = () => (
5353
);
5454

5555
CustomChromeSelector.story = {
56-
parameters: { loki: { chromeSelector: '#inner' } },
56+
parameters: { loki: { chromeSelector: '#inner' }, nonLokiParam: 'test123' },
5757
};

packages/browser/src/get-stories.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,28 @@ const getStories = (window) => {
99
"Unable to get stories. Try adding `import 'loki/configure-react'` to your .storybook/preview.js file."
1010
);
1111
}
12+
const blockedParams = [
13+
'actions',
14+
'argTypes',
15+
'backgrounds',
16+
'controls',
17+
'docs',
18+
'framework',
19+
'storySource',
20+
];
21+
1222
return getStorybook()
1323
.map((component) => ({
1424
id: component.id,
1525
kind: component.kind,
1626
story: component.story,
17-
parameters: component.parameters.loki || {},
27+
parameters: Object.fromEntries(
28+
Object.entries(component.parameters || {}).filter(
29+
([key]) => !key.startsWith('__') && !blockedParams.includes(key)
30+
)
31+
),
1832
}))
19-
.filter(({ parameters }) => !parameters.skip);
33+
.filter(({ parameters }) => !parameters.loki || !parameters.loki.skip);
2034
};
2135

2236
module.exports = getStories;

packages/integration-react-native/src/configure-storybook-react-native.js

+12-6
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,18 @@ async function configureStorybook() {
156156
});
157157

158158
on('getStories', () => {
159-
const stories = storybook.map((component) => ({
160-
id: component.id,
161-
kind: component.kind,
162-
story: component.story,
163-
parameters: component.parameters.loki || {},
164-
}));
159+
const stories = storybook
160+
.map((component) => ({
161+
id: component.id,
162+
kind: component.kind,
163+
story: component.story,
164+
parameters: Object.fromEntries(
165+
Object.entries(component.parameters || {}).filter(
166+
([key]) => !key.startsWith('__')
167+
)
168+
),
169+
}))
170+
.filter(({ parameters }) => !parameters.loki || !parameters.loki.skip);
165171
emit('setStories', { stories });
166172
});
167173

packages/runner/src/commands/test/compare-screenshot.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ async function compareScreenshot(
1010
tolerance,
1111
configurationName,
1212
kind,
13-
story
13+
story,
14+
parameters
1415
) {
1516
const { outputPath, referencePath, diffPath } = getOutputPaths(
1617
options,
1718
configurationName,
1819
kind,
19-
story
20+
story,
21+
parameters
2022
);
2123
const referenceExists = await fs.pathExists(referencePath);
2224
const shouldUpdateReference =

packages/runner/src/commands/test/compare-screenshot.spec.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ describe('compareScreenshot', () => {
1515
const kind = 'Kind';
1616
const story = 'Story';
1717
const filename = `${configurationName}_${kind}_${story}.png`;
18+
const parameters = {};
1819

1920
const executeWithOptions = (options) =>
2021
compareScreenshot(
@@ -23,7 +24,8 @@ describe('compareScreenshot', () => {
2324
tolerance,
2425
configurationName,
2526
kind,
26-
story
27+
story,
28+
parameters
2729
);
2830

2931
describe('reference image is missing', () => {

packages/runner/src/commands/test/create-baseline-limited-batch-builder.js

+39-37
Original file line numberDiff line numberDiff line change
@@ -7,43 +7,45 @@ const FILL_RATE = 0.9;
77
const DEFAULT_SIZE_LIMIT =
88
FILL_RATE * (LAMBDA_PAYLOAD_LIMIT / BASE64_ENCODING_OVERHEAD);
99

10-
const createBaselineLimitedBatchBuilder =
11-
(options, baselineSizeLimit = DEFAULT_SIZE_LIMIT) =>
12-
(tasks, batchSize) => {
13-
let currentBatch = [];
14-
let accumulatedBatchSize = 0;
15-
let lastFileSize = 200 * 1024;
16-
const batches = [currentBatch];
17-
for (let i = 0; i < tasks.length; i++) {
18-
const task = tasks[i];
19-
const { referencePath } = getOutputPaths(
20-
options,
21-
task.task.configurationName,
22-
task.task.kind,
23-
task.task.story
24-
);
25-
let size;
26-
try {
27-
const stat = fs.statSync(referencePath);
28-
// eslint-disable-next-line prefer-destructuring
29-
size = stat.size;
30-
lastFileSize = size;
31-
} catch (e) {
32-
size = lastFileSize;
33-
}
34-
if (
35-
currentBatch.length >= batchSize ||
36-
(accumulatedBatchSize + size > baselineSizeLimit &&
37-
currentBatch.length !== 0)
38-
) {
39-
currentBatch = [];
40-
accumulatedBatchSize = 0;
41-
batches.push(currentBatch);
42-
}
43-
currentBatch.push(task);
44-
accumulatedBatchSize += size;
10+
const createBaselineLimitedBatchBuilder = (
11+
options,
12+
baselineSizeLimit = DEFAULT_SIZE_LIMIT
13+
) => (tasks, batchSize) => {
14+
let currentBatch = [];
15+
let accumulatedBatchSize = 0;
16+
let lastFileSize = 200 * 1024;
17+
const batches = [currentBatch];
18+
for (let i = 0; i < tasks.length; i++) {
19+
const task = tasks[i];
20+
const { referencePath } = getOutputPaths(
21+
options,
22+
task.task.configurationName,
23+
task.task.kind,
24+
task.task.story,
25+
task.task.parameters
26+
);
27+
let size;
28+
try {
29+
const stat = fs.statSync(referencePath);
30+
// eslint-disable-next-line prefer-destructuring
31+
size = stat.size;
32+
lastFileSize = size;
33+
} catch (e) {
34+
size = lastFileSize;
4535
}
46-
return batches;
47-
};
36+
if (
37+
currentBatch.length >= batchSize ||
38+
(accumulatedBatchSize + size > baselineSizeLimit &&
39+
currentBatch.length !== 0)
40+
) {
41+
currentBatch = [];
42+
accumulatedBatchSize = 0;
43+
batches.push(currentBatch);
44+
}
45+
currentBatch.push(task);
46+
accumulatedBatchSize += size;
47+
}
48+
return batches;
49+
};
4850

4951
module.exports = createBaselineLimitedBatchBuilder;

packages/runner/src/commands/test/create-baseline-limited-batch-builder.spec.js

+14-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ const mockFsWithTasks = (mockTasks) => {
1313
mockOptions,
1414
task.task.configurationName,
1515
task.task.kind,
16-
task.task.story
16+
task.task.story,
17+
task.task.parameters
1718
);
1819
acc[referencePath] = task.size;
1920
return acc;
@@ -35,6 +36,7 @@ const generateTasks = (sizes) =>
3536
configurationName: 'configuration',
3637
kind: 'kind',
3738
story: `story ${i + 1}`,
39+
parameters: {},
3840
},
3941
}));
4042

@@ -54,7 +56,11 @@ describe('createBaselineLimitedBatchBuilder', () => {
5456
const tasks = generateTasks(new Array(10).fill(limit / 2));
5557
mockFsWithTasks(tasks);
5658
expectBatchLengths({ options, limit, tasks, batchSize }).toEqual([
57-
2, 2, 2, 2, 2,
59+
2,
60+
2,
61+
2,
62+
2,
63+
2,
5864
]);
5965
});
6066

@@ -77,7 +83,12 @@ describe('createBaselineLimitedBatchBuilder', () => {
7783
const tasks = generateTasks([1, 2, 3, 4, 5, 5, 4, 7, 8, 11]);
7884
mockFsWithTasks(tasks);
7985
expectBatchLengths({ options, limit, tasks, batchSize }).toEqual([
80-
4, 2, 1, 1, 1, 1,
86+
4,
87+
2,
88+
1,
89+
1,
90+
1,
91+
1,
8192
]);
8293
});
8394
});

packages/runner/src/commands/test/get-output-paths.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ const SLUGIFY_OPTIONS = {
99
const defaultFileNameFormatter = ({ configurationName, kind, story }) =>
1010
slugify(`${configurationName} ${kind} ${story}`, SLUGIFY_OPTIONS);
1111

12-
function getOutputPaths(options, configurationName, kind, story) {
12+
function getOutputPaths(options, configurationName, kind, story, parameters) {
1313
const getBaseName = options.fileNameFormatter || defaultFileNameFormatter;
14-
const basename = getBaseName({ configurationName, kind, story });
14+
const basename = getBaseName({ configurationName, kind, story, parameters });
1515
const filename = `${basename}.png`;
1616
const outputPath = `${options.outputDir}/${filename}`;
1717
const referencePath = `${options.referenceDir}/${filename}`;

packages/runner/src/commands/test/test-batch.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ function testBatch(target, batch, options, tolerance) {
5555
tolerance,
5656
configurationName,
5757
kind,
58-
story
58+
story,
59+
parameters
5960
);
6061
}
6162
);

packages/target-chrome-core/src/create-chrome-target.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ function createChromeTarget(
385385
tabOptions = Object.assign(tabOptions, presets[configuration.preset]);
386386
}
387387
const selector =
388-
parameters.chromeSelector ||
388+
(parameters.loki && parameters.loki.chromeSelector) ||
389389
configuration.chromeSelector ||
390390
options.chromeSelector;
391391
const url = getStoryUrl(storyId);

0 commit comments

Comments
 (0)