Skip to content
This repository was archived by the owner on Jun 12, 2024. It is now read-only.

Commit d0171f3

Browse files
weissguyljharb
authored andcommitted
[New] forEachVariation: add projectName to descriptor
1 parent dd22dfe commit d0171f3

4 files changed

Lines changed: 29 additions & 3 deletions

File tree

src/traversal/forEachProjectVariation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export default function forEachProjectVariation(consumer, {
3535
projectConfig,
3636
{ getExtras, getDescriptor, projectRoot },
3737
)((descriptor) => {
38-
forEachVariation(descriptor, consumer, callback);
38+
forEachVariation({ projectName, ...descriptor }, consumer, callback);
3939
});
4040
});
4141
};

src/traversal/forEachVariation.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ export default function forEachVariation(descriptor, consumer, callback) {
88

99
const {
1010
component,
11+
projectName,
1112
createdAt: rootCreatedAt,
1213
usage,
1314
options: allRootConsumerOptions = {},
1415
metadata = {},
1516
variations,
1617
} = descriptor;
18+
1719
const { [consumer]: rootConsumerOptions = {} } = allRootConsumerOptions || {};
1820

1921
// this consumer is disabled
@@ -45,6 +47,7 @@ export default function forEachVariation(descriptor, consumer, callback) {
4547

4648
const newVariation = {
4749
componentName,
50+
projectName,
4851
title,
4952
component,
5053
usage,

test/traversal/forEachProjectVariation.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ describe('forEachProjectVariation', () => {
6666
expect(forEachVariation).toHaveBeenCalledTimes(mockProjectNames.length);
6767
const { calls } = forEachVariation.mock;
6868

69-
calls.forEach((args) => {
70-
expect(args).toEqual([mockDescriptor, consumer, callback]);
69+
calls.forEach((args, idx) => {
70+
expect(args).toEqual([{ projectName: mockProjectNames[idx], ...mockDescriptor }, consumer, callback]);
7171
});
7272
});
7373
});

test/traversal/forEachVariation.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,30 @@
11
import forEachVariation from '../../src/traversal/forEachVariation';
22

3+
const mockDescriptor = {
4+
component: 'mock-component',
5+
projectName: 'mock-project',
6+
createdAt: 'timestamp',
7+
usage: 'mock-usage',
8+
metadata: { foo: 'bar' },
9+
variations: [],
10+
};
11+
312
describe('forEachVariation', () => {
413
it('is a function', () => {
514
expect(typeof forEachVariation).toBe('function');
615
});
16+
17+
describe('callback function', () => {
18+
it('passes in the correct mockDescriptor data', () => {
19+
const callback = (newVariation) => {
20+
Object.keys(mockDescriptor).forEach((property) => {
21+
if (property === 'variations') {
22+
return;
23+
}
24+
expect(mockDescriptor[property]).toEqual(newVariation[property]);
25+
});
26+
};
27+
forEachVariation(mockDescriptor, {}, callback);
28+
});
29+
});
730
});

0 commit comments

Comments
 (0)