Skip to content

Commit

Permalink
unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
hassanad94 committed Sep 5, 2023
1 parent 03bb6f7 commit 6e457cc
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ export const ReferencePicker: React.FC<ReferencePickerProps<GenericContentWithIs
if (props.repository.schemas.isContentFromType<Image>(item, 'Image')) {
return (
<img
alt=""
data-test="reference-selection-image"
alt={item.DisplayName}
src={`${props.repository.configuration.repositoryUrl}${item.Path}`}
style={{ width: '3em', height: '3em', objectFit: 'scale-down' }}
/>
Expand Down
4 changes: 4 additions & 0 deletions packages/sn-controls-react/test/__mocks__/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ export const testArticle = {
export const testFolder = {
Type: 'Folder',
} as GenericContent

export const testImage = {
Type: 'Image',
} as GenericContent
34 changes: 32 additions & 2 deletions packages/sn-controls-react/test/reference-grid.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { ReferencePicker } from '../src/fieldcontrols/reference-grid/reference-p

const defaultSettings = {
Type: 'ReferenceFieldSetting',
AllowedTypes: ['User', 'Group'],
AllowedTypes: ['User', 'Group', 'Image'],
SelectionRoots: ['/Root/IMS', '/Root'],
Name: 'Members',
FieldClassName: 'SenseNet.ContentRepository.Fields.ReferenceField',
Expand All @@ -43,6 +43,15 @@ const userContent = {
},
}

const imageContent = {
Name: 'Test Image',
Path: '/Root/IMS/Public/alba',
DisplayName: 'Test Image',
Id: 4830,
Type: 'Image',
Enabled: true,
}

const repository: any = {
load: jest.fn((props) => {
return { d: userContent }
Expand Down Expand Up @@ -227,7 +236,7 @@ describe('Reference grid field control', () => {
)
await sleepAsync(0)

wrapper.find(ReferencePicker).prop('handleSubmit')([{ Path: '/', Name: 'Jane Doe', Id: 1234, Type: 'User' }])
wrapper.find(ReferencePicker).prop('handleSubmit')!([{ Path: '/', Name: 'Jane Doe', Id: 1234, Type: 'User' }])

// Jane Doe + add reference
expect(wrapper.update().find(DefaultItemTemplate)).toHaveLength(2)
Expand All @@ -247,5 +256,26 @@ describe('Reference grid field control', () => {

expect(wrapper.update().find(Avatar).text()).toBe('A.M')
})

it('should render img tag if type is image', async () => {
const repo = {
loadCollection: jest.fn(() => {
return { d: { results: [{ ...imageContent }] } }
}),
schemas: {
isContentFromType: jest.fn((a, b) => b === 'Image'),
},
configuration: repository.configuration,
} as any
let wrapper: any
await act(async () => {
wrapper = mount(
<ReferencePicker repository={repo} fieldSettings={{ ...defaultSettings, Type: 'Image' }} path="" />,
)
})

expect(wrapper.update().find('img').prop('src')).toContain(imageContent.Path)
expect(wrapper.update().find('img').prop('alt')).toBe(imageContent.DisplayName)
})
})
})

0 comments on commit 6e457cc

Please sign in to comment.