From 09fad54e124bc9f2676d874b3195edf4493b2b2d Mon Sep 17 00:00:00 2001 From: blam Date: Wed, 19 May 2021 18:47:05 +0200 Subject: [PATCH] tests: added some tests for the new composability helpers Signed-off-by: blam --- plugins/scaffolder/src/components/Router.tsx | 12 +- .../src/extensions/helpers.test.tsx | 107 ++++++++++++++++++ 2 files changed, 115 insertions(+), 4 deletions(-) create mode 100644 plugins/scaffolder/src/extensions/helpers.test.tsx diff --git a/plugins/scaffolder/src/components/Router.tsx b/plugins/scaffolder/src/components/Router.tsx index d80e75cd38..164182036d 100644 --- a/plugins/scaffolder/src/components/Router.tsx +++ b/plugins/scaffolder/src/components/Router.tsx @@ -14,7 +14,7 @@ * limitations under the License. */ -import React from 'react'; +import React, { useMemo } from 'react'; import { Routes, Route, useOutlet } from 'react-router'; import { ScaffolderPage } from './ScaffolderPage'; import { TemplatePage } from './TemplatePage'; @@ -31,9 +31,13 @@ import { collectComponentData, collectChildren } from '../extensions/helpers'; export const Router = () => { const outlet = useOutlet(); - const fieldExtensions = collectComponentData( - collectChildren(outlet, FIELD_EXTENSION_WRAPPER_KEY).flat(), - FIELD_EXTENSION_KEY, + const fieldExtensions = useMemo( + () => + collectComponentData( + collectChildren(outlet, FIELD_EXTENSION_WRAPPER_KEY).flat(), + FIELD_EXTENSION_KEY, + ), + [outlet], ); return ( diff --git a/plugins/scaffolder/src/extensions/helpers.test.tsx b/plugins/scaffolder/src/extensions/helpers.test.tsx new file mode 100644 index 0000000000..ed3b74a210 --- /dev/null +++ b/plugins/scaffolder/src/extensions/helpers.test.tsx @@ -0,0 +1,107 @@ +/* + * Copyright 2021 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import React from 'react'; +import { collectComponentData, collectChildren } from './helpers'; +import { attachComponentData } from '@backstage/core'; + +describe('Extension Helpers', () => { + const createElementWithComponentData = ({ + type, + data, + }: { + type: string; + data: any; + }) => { + const element: React.ComponentType = () => null; + attachComponentData(element, type, data); + return element; + }; + + describe('collectChildren', () => { + it('should return the children of the component which has the correct componentData flag', () => { + const SearchElement = createElementWithComponentData({ + type: 'find.me', + data: {}, + }); + + const DontCareAboutme = createElementWithComponentData({ + type: 'dont.find.me', + data: {}, + }); + + const child1 = ( +
+ hello +
+ ); + + const child2 = ( +
+

Hello2

+
+ ); + + const testCase = ( +
+ + {child1} + {child1} + + {child2} + +

Hello!

+ {child1} +
+
+ ); + + const children = collectChildren(testCase, 'find.me'); + + expect(children).toEqual([[child1, child1], child2, child1]); + }); + }); + + describe('collectComponentData', () => { + it('should return the componentData for particular nodes', () => { + const componentData1 = { help: 'im something' }; + const componentData2 = { help: 'im something else' }; + + const FirstElement = createElementWithComponentData({ + type: 'find.me', + data: componentData1, + }); + + const SecondElement = createElementWithComponentData({ + type: 'dont.find.me', + data: componentData2, + }); + + const testCase = [ + , + , + , + , + ]; + const returnedData = collectComponentData(testCase, 'find.me'); + + expect(returnedData).toEqual([ + componentData1, + componentData1, + componentData1, + ]); + }); + }); +});