Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Laucher ensureKonnectorFolder tests #1180

Merged
merged 1 commit into from
Feb 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 25 additions & 71 deletions src/libs/Launcher.spec.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import { models } from 'cozy-client'
import { saveFiles } from 'cozy-clisk'

import Launcher from './Launcher'
import { saveCredential, getSlugAccountIds } from './keychain'

jest.mock('./keychain')
jest.mock('cozy-clisk')

const existingMagicFolder = [
{
path: '/Administratif',
created_at: '2023-03-02T14:57:07.661588+01:00'
jest.mock('cozy-client', () => ({
...jest.requireActual('cozy-client'),
models: {
konnectorFolder: {
ensureKonnectorFolder: jest.fn()
}
}
]
}))

describe('Launcher', () => {
describe('ensureAccountTriggerAndLaunch', () => {
Expand All @@ -29,24 +31,12 @@ describe('Launcher', () => {
folder_to_save: 'oldfolderid'
}
}
models.konnectorFolder.ensureKonnectorFolder.mockResolvedValue({
_id: 'createdfolderid'
})
const job = { _id: 'testjob' }
const mockClient = {
collection: () => mockClient,
findReferencedBy: jest
.fn()
.mockResolvedValue({ included: existingMagicFolder }),
statByPath: jest.fn().mockRejectedValueOnce({ status: 404 }),
add: jest.fn(),
addReferencesTo: jest.fn(),
createDirectoryByPath: jest.fn().mockResolvedValueOnce({
data: {
_id: 'createdfolderid'
}
}),
query: jest.fn().mockResolvedValue({ included: [] }),
get: jest.fn().mockResolvedValue({ data: {} }),
statById: jest.fn().mockResolvedValue({ data: {} }),
ensureDirectoryExists: jest.fn(),
getInstanceOptions: jest.fn().mockReturnValueOnce({ locale: 'fr' }),
save: jest.fn().mockResolvedValueOnce({
data: { message: { folder_to_save: 'newfolderid' } }
Expand Down Expand Up @@ -304,6 +294,7 @@ describe('Launcher', () => {
const konnector = {
slug: 'testkonnectorslug',
id: 'testkonnectorid',
_id: 'testkonnectorid',
name: 'Test Konnector'
}
const trigger = {
Expand All @@ -317,23 +308,7 @@ describe('Launcher', () => {
message: { account: 'testaccountid', folder_to_save: 'testfolderid' }
}
const client = {
collection: doctype => {
if (doctype === 'io.cozy.permissions') {
return { add: jest.fn() }
}
return client
},
addReferencesTo: jest.fn(),
statByPath: jest
.fn()
.mockImplementation(path => ({ data: { _id: path } })),
findReferencedBy: jest
.fn()
.mockResolvedValue({ included: existingMagicFolder }),
getInstanceOptions: jest.fn().mockReturnValue({ locale: 'fr' }),
ensureDirectoryExists: jest.fn(),
get: jest.fn().mockResolvedValue({ data: {} }),
statById: jest.fn().mockResolvedValue({ data: {} }),
queryAll: jest.fn().mockResolvedValue([
{
_id: 'tokeep',
Expand All @@ -358,6 +333,9 @@ describe('Launcher', () => {
trigger,
job
})
models.konnectorFolder.ensureKonnectorFolder.mockResolvedValue({
_id: '/Administratif/Test Konnector/testaccountid'
})
saveFiles.mockRejectedValueOnce(new Error('MAIN_FOLDER_REMOVED'))

await launcher.saveFiles([{}], {})
Expand Down Expand Up @@ -394,41 +372,17 @@ describe('Launcher', () => {
sourceAccount: 'testaccountid',
sourceAccountIdentifier: 'testsourceaccountidentifier'
})
expect(client.save).toHaveBeenCalledTimes(1)
expect(client.save).toHaveBeenNthCalledWith(1, {
_type: 'io.cozy.triggers',
message: {
account: 'testaccountid',
folder_to_save: '/Administrative/Test Konnector/testaccountid'
}

expect(
models.konnectorFolder.ensureKonnectorFolder
).toHaveBeenCalledTimes(1)
expect(
models.konnectorFolder.ensureKonnectorFolder
).toHaveBeenNthCalledWith(1, client, {
konnector,
account: { _id: 'testaccountid' },
lang: 'fr'
})
expect(client.queryAll).toHaveBeenCalledTimes(2)
expect(client.queryAll).toHaveBeenNthCalledWith(
1,
expect.objectContaining({
doctype: 'io.cozy.files',
selector: {
cozyMetadata: {
createdByApp: 'testkonnectorslug',
sourceAccountIdentifier: 'testsourceaccountidentifier'
},
trashed: false
}
})
)
expect(client.queryAll).toHaveBeenNthCalledWith(
2,
expect.objectContaining({
doctype: 'io.cozy.files',
selector: {
cozyMetadata: {
createdByApp: 'testkonnectorslug',
sourceAccountIdentifier: 'testsourceaccountidentifier'
},
trashed: false
}
})
)
})
})
})
Loading