Skip to content

Commit

Permalink
test: Test the opening of a note
Browse files Browse the repository at this point in the history
  • Loading branch information
Crash-- committed Feb 20, 2020
1 parent 6ee4ea6 commit a020052
Showing 1 changed file with 11 additions and 85 deletions.
96 changes: 11 additions & 85 deletions src/drive/web/modules/navigation/FileExplorer.spec.jsx
@@ -1,7 +1,8 @@
import { handleFileOpen } from './FileExplorer'
import { models } from 'cozy-client'

import { openLocalFile } from 'drive/mobile/modules/offline/duck'
import { fetchUrlToOpenANote } from 'drive/web/modules/drive/files'

jest.mock('cozy-ui/transpiled/react/utils/color', () => ({
getCssVariableValue: () => '#fff'
}))
Expand All @@ -11,6 +12,12 @@ jest.mock('drive/mobile/modules/offline/duck', () => {
openLocalFile: jest.fn()
}
})
jest.mock('drive/web/modules/drive/files', () => {
return {
...require.requireActual('drive/web/modules/drive/files'),
fetchUrlToOpenANote: jest.fn()
}
})
jest.mock('cozy-client')

const client = {
Expand Down Expand Up @@ -41,22 +48,6 @@ const note = {
}
}

const sharedNote = {
_id: '3',
id: '3',
type: 'file',
name: 'test.cozy-note',
metadata: {
content: 'content',
schema: [],
title: 'title',
version: '0'
},
cozyMetadata: {
createdOn: 'http://q.cozy.tools/'
}
}

const mockedDispatch = x => x

const mockedRouter = {
Expand All @@ -68,17 +59,6 @@ const mockedRouter = {
}
}

const notesNotInstalled = {
apps: {
data: []
}
}

const notesInstalled = {
apps: {
data: [{ slug: 'notes' }, { slug: 'store' }]
}
}
describe('FileExplorer', () => {
describe('handleFileOpen', () => {
const originalLocation = global.window.location
Expand Down Expand Up @@ -109,69 +89,15 @@ describe('FileExplorer', () => {
)
expect(mockedRouter.router.push).toHaveBeenCalledWith('/folder/a/file/1')
})
it('should call the method to generate the URL to install the notes app from the store', () => {
it('should call the stack route to open a note', async () => {
const isAvailableOffline = false
models.applications.isInstalled = jest.fn().mockReturnValue(false)
models.applications.getStoreInstallationURL = jest.fn()
handleFileOpen(
note,
isAvailableOffline,
{ ...mockedRouter, ...notesNotInstalled, client },
mockedDispatch
)
expect(models.applications.getStoreInstallationURL).toHaveBeenCalled()
})
it('should call the method to get the Notes url', () => {
const isAvailableOffline = false
models.applications.isInstalled.mockReturnValue({
links: { related: 'http://notes.foo.bar/' }
})
models.applications.getUrl = jest
.fn()
.mockReturnValue('http://notes.foo.bar/')
global.window = Object.create(window)
const url = 'http://notes.foo.bar/#/n/' + note._id
delete global.window.location
Object.defineProperty(window, 'location', {
value: {
href: url
},
writable: true
})
handleFileOpen(
note,
isAvailableOffline,
{ ...mockedRouter, ...notesInstalled, client },
{ ...mockedRouter, client },
mockedDispatch
)
expect(models.applications.getUrl).toHaveBeenCalled()
expect(window.location.href).toEqual(url)
expect(fetchUrlToOpenANote).toHaveBeenCalled()
})
})

it('should call the router.push if the returned URL is empty', () => {
const isAvailableOffline = false
models.applications.getStoreInstallationURL.mockReturnValue('')

handleFileOpen(
note,
isAvailableOffline,
{ ...mockedRouter, ...notesNotInstalled, client },
mockedDispatch
)
expect(mockedRouter.router.push).toHaveBeenCalledWith('/folder/a/file/2')
})

it('should call the router.push if the note is not mine', () => {
const isAvailableOffline = false
models.applications.getStoreInstallationURL.mockReturnValue('')

handleFileOpen(
sharedNote,
isAvailableOffline,
{ ...mockedRouter, ...notesInstalled, client },
mockedDispatch
)
expect(mockedRouter.router.push).toHaveBeenCalledWith('/folder/a/file/3')
})
})

0 comments on commit a020052

Please sign in to comment.