Skip to content

Commit

Permalink
Improve and test as TS
Browse files Browse the repository at this point in the history
  • Loading branch information
sneridagh committed Feb 20, 2024
1 parent c2bd017 commit 81dfe61
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
9 changes: 4 additions & 5 deletions packages/registry/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class Config {
}

getComponent(
options: { name: string; dependencies: string[] | string } | string,
options: { name: string; dependencies?: string[] | string } | string,
): GetComponentResult {
if (typeof options === 'object') {
const { name, dependencies = '' } = options;
Expand All @@ -148,7 +148,7 @@ class Config {

registerComponent(options: {
name: string;
dependencies: string[] | string;
dependencies?: string[] | string;
component: React.ComponentType;
}) {
const { name, component, dependencies = '' } = options;
Expand Down Expand Up @@ -192,9 +192,8 @@ class Config {
// TODO: Cover ZCA use case, where if more predicates, more specificity wins if all true.
// Let's keep it simple here and stick to the registered order.
for (const slotComponent of data[slotName].toReversed()) {
const isPredicateTrueFound = slotComponent.predicates.reduce(
(acc, value) => acc && value(args),
true,
const isPredicateTrueFound = slotComponent.predicates.every(
(predicate) => predicate(args),
);
// If all the predicates are truthy
if (isPredicateTrueFound) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,14 @@ describe.only('Slots registry', () => {
});

// type Predicate = (predicateValues: unknown) = (predicateValues, args) => boolean
const RouteConditionTrue = () => () => true;
const RouteConditionFalse = () => () => false;
const ContentTypeConditionTrue = () => () => true;
const ContentTypeConditionFalse = () => () => false;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const RouteConditionTrue = (route) => () => true;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const RouteConditionFalse = (route) => () => false;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const ContentTypeConditionTrue = (contentType) => () => true;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const ContentTypeConditionFalse = (contentType) => () => false;

it('registers two slot components with predicates - registered components order is respected', () => {
config.registerSlotComponent({
Expand Down Expand Up @@ -187,7 +191,7 @@ describe.only('Slots registry', () => {
],
});

expect(config.getSlot('toolbar')).toEqual([
expect(config.getSlot('toolbar', {})).toEqual([
'this is a toolbar component with two truth-ish predicates',
]);
});
Expand All @@ -210,7 +214,7 @@ describe.only('Slots registry', () => {
],
});

expect(config.getSlot('toolbar')).toEqual([]);
expect(config.getSlot('toolbar', {})).toEqual([]);
});

it('registers 2 + 2 slot components with predicates - No registered component have a truthy predicate', () => {
Expand Down Expand Up @@ -247,7 +251,7 @@ describe.only('Slots registry', () => {
ContentTypeConditionFalse(['News Item']),
],
});
expect(config.getSlot('toolbar')).toEqual([]);
expect(config.getSlot('toolbar', {})).toEqual([]);
});

it('registers 2 + 2 slot components with predicates - One truthy predicate per set', () => {
Expand Down Expand Up @@ -284,7 +288,7 @@ describe.only('Slots registry', () => {
ContentTypeConditionTrue(['News Item']),
],
});
expect(config.getSlot('toolbar')).toEqual([
expect(config.getSlot('toolbar', {})).toEqual([
'this is a toolbar save component with a true predicate',
'this is a toolbar edit component with true predicate',
]);
Expand Down

0 comments on commit 81dfe61

Please sign in to comment.