Skip to content

Commit

Permalink
test: add test on normalize feature
Browse files Browse the repository at this point in the history
  • Loading branch information
trollepierre committed Aug 10, 2022
1 parent 1b38dcb commit 5123093
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import React from 'react'
import { render } from '@testing-library/react'
import { render, waitFor } from '@testing-library/react'
import SuggestionProvider from './SuggestionProvider'
import { dummyFile, dummyNote } from 'test/dummies/dummyFile'

const mockFindAll = jest.fn().mockReturnValue([])
const parentFolder = dummyFile({ _id: 'id-file' })
const folder = dummyFile({ dir_id: 'id-file' })
const note = dummyNote({
dir_id: 'id-file',
name: 'name.cozy-note'
})
const mockFindAll = jest.fn().mockReturnValue([parentFolder, folder, note])
const mockClient = {
collection: jest.fn().mockReturnValue({ findAll: mockFindAll })
}
Expand All @@ -12,27 +19,34 @@ jest.mock('cozy-client', () => {
return {
...jest.requireActual('cozy-client'),
withClient: Component => () => {
const intent = { attributes: { client: mockIntentAttributesClient } }
const intent = {
_id: 'id_intent',
attributes: { client: mockIntentAttributesClient }
}
return <Component client={mockClient} intent={intent}></Component>
}
}
})
jest.mock('./iconContext', () => ({ getIconUrl: () => 'iconUrl' }))

describe('SuggestionProvider', () => {
it('should query all files to display fuzzy suggestion', () => {
// Given
let events = {}
let events = {}
let event

beforeEach(() => {
window.addEventListener = jest.fn((event, callback) => {
events[event] = callback
})
window.postMessage = jest.fn()

render(<SuggestionProvider />)
const event = {
window.parent.postMessage = jest.fn()
event = {
origin: mockIntentAttributesClient,
data: { query: 'query', id: 'id' }
data: { query: 'name', id: 'id' }
}
})

it('should query all files to display fuzzy suggestion', () => {
// Given
render(<SuggestionProvider />)

// When
events.message(event)
Expand All @@ -57,4 +71,41 @@ describe('SuggestionProvider', () => {
}
)
})

it('should provide onSelect with open url when file is not a note + and function when it is a note', async () => {
// Given
render(<SuggestionProvider />)

// When
events.message(event)

// Then
await waitFor(() => {
expect(window.parent.postMessage).toHaveBeenCalledWith(
{
id: 'id',
suggestions: [
{
icon: 'iconUrl',
id: 'id-file',
onSelect: 'open:http://localhost/#/folder/id-file',
subtitle: '/path',
term: 'name',
title: 'name'
},
{
icon: 'iconUrl',
id: 'id-file',
onSelect: expect.any(Function),
subtitle: '/path',
term: 'name.cozy-note',
title: 'name.cozy-note'
}
],
type: 'intent-id_intent:data'
},
'intent-attributes-client'
)
})
})
})
33 changes: 33 additions & 0 deletions test/dummies/dummyFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Create a dummy file, with overridden value of given param
*
* @param file
* @returns {*&{path: string, name: string, icon: string, id: string, _id: string, dir_id: string, type: string}} a dummy file
*/
export const dummyFile = file => ({
name: 'name',
id: 'id-file',
_id: 'id-file',
icon: 'icon',
path: '/path',
type: 'directory',
...file
})

/**
* Create a dummy note, with overridden value of given param
*
* @param note
* @returns {*&{path: string, name: string, icon: string, id: string, _id: string, dir_id: string, type: string}} a dummy note
*/
export const dummyNote = note => ({
...dummyFile(),
type: 'file',
metadata: {
content: '',
schema: '',
title: '',
version: ''
},
...note
})

0 comments on commit 5123093

Please sign in to comment.