Skip to content

Commit

Permalink
test(suggestion): check queryAll is correctly called with good params
Browse files Browse the repository at this point in the history
  • Loading branch information
trollepierre committed Aug 5, 2022
1 parent 79b220f commit 63de61f
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
11 changes: 9 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"no-console": 1,
"no-param-reassign": "error",
"react-hooks/exhaustive-deps": "error"

},
"globals": {
"fixture": false
Expand All @@ -13,5 +12,13 @@
"react": {
"version": "detect"
}
}
},
"overrides": [
{
"files": ["*.spec.js[x]"],
"rules": {
"react/display-name": "off"
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import React from 'react'
import { render } from '@testing-library/react'
import SuggestionProvider from './SuggestionProvider'

let files = []
const mockClient = { queryAll: jest.fn().mockReturnValue(files) }
const mockIntentAttributesClient = 'intent-attributes-client'

jest.mock('cozy-client', () => {
return {
...jest.requireActual('cozy-client'),
withClient: Component => () => {
const 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 = {}
window.addEventListener = jest.fn((event, callback) => {
events[event] = callback
})
window.postMessage = jest.fn()

render(<SuggestionProvider />)
const event = {
origin: mockIntentAttributesClient,
data: { query: 'query', id: 'id' }
}

// When
events.message(event)

// Then
expect(mockClient.queryAll).toHaveBeenCalledWith({
doctype: 'io.cozy.files',
fields: ['_id', 'trashed', 'dir_id', 'name', 'path'],
limit: 1000,
partialFilter: {
_id: { $ne: 'io.cozy.files.trash-dir' },
trashed: { $or: [{ $exists: false }, { $eq: false }] }
},
selector: { _id: { $gt: null } }
})
})
})

0 comments on commit 63de61f

Please sign in to comment.