diff --git a/plugins/scaffolder/src/components/fields/OwnershipEntityRefPicker/OwnershipEntityRefPicker.test.tsx b/plugins/scaffolder/src/components/fields/OwnershipEntityRefPicker/OwnershipEntityRefPicker.test.tsx index eb2f165350..1c1027634a 100644 --- a/plugins/scaffolder/src/components/fields/OwnershipEntityRefPicker/OwnershipEntityRefPicker.test.tsx +++ b/plugins/scaffolder/src/components/fields/OwnershipEntityRefPicker/OwnershipEntityRefPicker.test.tsx @@ -13,3 +13,119 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + +import React from 'react'; +import { render } from '@testing-library/react'; +import { CatalogApi } from '@backstage/catalog-client'; +import { FieldProps } from '@rjsf/core'; +import { EntityPickerProps } from '../EntityPicker/schema'; +import { Entity } from '@backstage/catalog-model'; +import { OwnershipEntityRefPicker } from './OwnershipEntityRefPicker'; + +const makeUserEntity = ( + kind: string, + memberOf: string[], + name: string, +): Entity => ({ + apiVersion: 'scaffolder.backstage.io/v1beta3', + kind, + metadata: { name }, + spec: { + memberOf: memberOf, + }, +}); + +const makeGroupEntity = ( + kind: string, + members: string[], + name: string, +): Entity => ({ + apiVersion: 'scaffolder.backstage.io/v1beta3', + kind, + metadata: { name }, + spec: { + members: members, + }, +}); + +describe('', () => { + let entities: Entity[]; + const onChange = jest.fn(); + const schema = {}; + const required = false; + let uiSchema: EntityPickerProps['uiSchema']; + const rawErrors: string[] = []; + const formData = undefined; + + let props: FieldProps; + + const catalogApi: jest.Mocked = { + getLocationById: jest.fn(), + getEntityByName: jest.fn(), + addLocation: jest.fn(), + getLocationByRef: jest.fn(), + removeEntityByUid: jest.fn(), + } as any; + + beforeEach(() => { + entities = [ + makeUserEntity('User', ['group1', 'group2'], 'Bob'), + makeGroupEntity('Group', ['Alice', 'Dave'], 'group3'), + ]; + }); + + afterEach(() => jest.resetAllMocks()); + + it('should only return the groups a user is part of', async () => { + catalogApi.getEntityByRef.mockResolvedValueOnce(entities[0]); + + uiSchema = { 'ui:options': { catalogApi } }; + props = { + onChange, + schema, + required, + uiSchema, + rawErrors, + formData, + } as unknown as FieldProps; + + render(); + + // Assuming the component calls `onChange` with the groups a user is a part of + expect(onChange).toHaveBeenCalledWith(['group1', 'group2']); + }); + + it('should not return groups a user is not part of', async () => { + catalogApi.getEntityByRef.mockResolvedValueOnce(entities[1]); + + uiSchema = { 'ui:options': { catalogApi } }; + props = { + onChange, + schema, + required, + uiSchema, + rawErrors, + formData, + } as unknown as FieldProps; + + render(); + + // Assuming the component calls `onChange` with the groups a user is a part of + expect(onChange).not.toHaveBeenCalledWith(['group1', 'group2']); + }); + + it('should render without imploding', () => { + uiSchema = { 'ui:options': { catalogApi } }; + props = { + onChange, + schema, + required, + uiSchema, + rawErrors, + formData, + } as unknown as FieldProps; + + const { container } = render(); + expect(container).not.toBeNull(); + }); +});