Skip to content
Snippets Groups Projects
Commit b9f052df authored by Rémi PAPIN's avatar Rémi PAPIN
Browse files

Add fluid + fluidConfig + Konnector + userProfile and missing one fct on konnector service

parent 7e02de11
Branches
Tags
1 merge request!139Features/unit tests
import FluidService from './fluid.service'
import { Client } from 'cozy-client'
const mockClient = ({
find: jest.fn(),
query: jest.fn(),
create: jest.fn(),
destroy: jest.fn(),
} as unknown) as jest.Mocked<Client>
const mockData = {}
describe('FLuid service', () => {
const fluidService = new FluidService(mockClient)
describe('getFluidStatus', () => {
it('shoud return fluid status', async () => {
// mockClient.query.mockImplementation(() =>
// Promise.resolve({ data: mockData })
// )
const result = await fluidService.getFluidStatus()
expect(result).toEqual(mockData)
})
})
})
import ConfigService from './fluidConfig.service'
describe('ConfigService service', () => {
const configService = new ConfigService()
describe('getFluidConfig', () => {
it('shoud return an array with the fluid config', () => {
const result = configService.getFluidConfig()
expect(result).toBeDefined()
})
})
describe('getCronArgs', () => {
it('shoud return the args of a cron', () => {
const max = 5
const min = 5
const result = configService.getCronArgs(min, max)
expect(typeof result).toEqual('string')
})
})
})
import KonnectorService from './konnector.service'
import { Client, QueryDefinition } from 'cozy-client'
import TriggerService from 'services/triggers.service'
import { Trigger, TriggerState, Account } from 'models'
const mockTriggers = ({
getTrigger: jest.fn(),
fetchTriggerState: jest.fn(),
} as unknown) as jest.Mocked<TriggerService>
const mockClient = ({
find: jest.fn(),
query: jest.fn(),
create: jest.fn(),
destroy: jest.fn(),
save: jest.fn(),
getStackClient: jest.fn(),
} as unknown) as jest.Mocked<Client>
const mockAccount: Account = {
_id: 'a686348c1fc85f92033e19eb6b0042af',
// eslint-disable-next-line @typescript-eslint/camelcase
account_type: 'eglgrandlyon',
auth: {
// eslint-disable-next-line @typescript-eslint/camelcase
credentials_encrypted:
'bmFjbNDdeM1X3TDBNfSmJTWOsiMkZHLkhUZVdfIh/BlrsTpNKYKZ6p63eBOpXUBSok5cTJBNuP2fVwOZAPzVKAACO+FMC2sdZtgk/CDqxwOA/roQlg==',
login: 'test',
},
identifier: 'login',
}
const mockKonnectorData = [
{
_id: 'test',
name: 'test',
slug: 'test',
state: 'test',
},
]
const mockFind = new QueryDefinition()
const mockTriggerData: Trigger = {
_id: 'test',
type: 'test',
arguments: 'test',
worker: 'test',
message: {
account: 'test',
konnector: 'test',
},
}
const mockTriggerState: TriggerState = {
// eslint-disable-next-line @typescript-eslint/camelcase
trigger_id: 'test',
status: 'test',
// eslint-disable-next-line @typescript-eslint/camelcase
last_executed_job_id: 'test',
// eslint-disable-next-line @typescript-eslint/camelcase
last_execution: 'test',
// eslint-disable-next-line @typescript-eslint/camelcase
last_manual_execution: 'test',
// eslint-disable-next-line @typescript-eslint/camelcase
last_manual_job_id: 'test',
}
describe('KonnectorService service', () => {
const konnectorService = new KonnectorService(mockClient)
mockClient.find.mockReturnValue(mockFind)
mockClient.query.mockImplementation(() =>
Promise.resolve({ data: mockKonnectorData })
)
describe('getFluidConfig', () => {
it('shoud return an array with the fluid config', async () => {
const konnectorId = '0'
const result = await konnectorService.getKonnector(konnectorId)
expect(result).toEqual(mockKonnectorData[0])
})
})
describe('getKonnectorLastState', () => {
it('shoud return the triggerState or null', async () => {
mockTriggers.getTrigger.mockImplementation(() =>
Promise.resolve(mockTriggerData)
)
mockTriggers.fetchTriggerState.mockImplementation(() =>
Promise.resolve(mockTriggerState)
)
// mockClient.getStackClient.mockImplementation(() =>
// Promise.resolve(mockTriggerState)
// )
const result = await konnectorService.getKonnectorLastState(
mockKonnectorData[0],
mockAccount
)
expect(result).toEqual({})
})
})
describe('createIndexKonnector', () => {
it('shoud return the query Result', async () => {
const result = await konnectorService.createIndexKonnector()
expect(result).toEqual({ data: mockKonnectorData })
})
})
})
...@@ -39,9 +39,11 @@ export default class TriggerService { ...@@ -39,9 +39,11 @@ export default class TriggerService {
'TriggersServices : createTrigger - _triggerAttributes not found' 'TriggersServices : createTrigger - _triggerAttributes not found'
) )
} }
console.log(triggerAttributes)
const trigger = await triggersMutations(this._client).createTrigger( const trigger = await triggersMutations(this._client).createTrigger(
triggerAttributes triggerAttributes
) )
console.log(trigger)
return trigger return trigger
} }
......
import UserProfileService from './userProfile.service'
import { Client, QueryDefinition } from 'cozy-client'
const mockClient = ({
find: jest.fn(),
query: jest.fn(),
create: jest.fn(),
destroy: jest.fn(),
save: jest.fn(),
} as unknown) as jest.Mocked<Client>
const mockUserProfile = [
{
_id: 'a56c80fcf9fadbe85423e448fc005a9a',
_rev: '36-6c6bd449107c2d1c86ae7f58094c7503',
challengeTypeHash: '1136feb6185c7643e071d14180c0e95782aa4ba3',
cozyMetadata: {
createdAt: '2020-11-03T08:56:10.311Z',
createdByApp: 'ecolyo',
createdByAppVersion: '0.2.1',
metadataVersion: 1,
updatedAt: '2020-11-09T10:27:30.156Z',
updatedByApps: [
{
date: '2020-11-09T10:27:30.156Z',
slug: 'ecolyo',
version: '0.2.1',
},
],
},
ecogestureHash: '9798a0aaccb47cff906fc4931a2eff5f9371dd8b',
haveSeenFavoriteModal: true,
haveSeenOldFluidModal: '2020-11-09T11:27:30.073+01:00',
id: 'a56c80fcf9fadbe85423e448fc005a9a',
isFirstConnection: false,
level: 3,
notificationEcogesture: ['0085', '0082', '0041', '0064', '0045'],
report: {
haveSeenLastReport: true,
monthlyReportDate: '2020-11-03T00:00:00.000+01:00',
sendReportNotification: false,
},
},
]
const mockFind = new QueryDefinition()
describe('UserProfile service', () => {
const userProfileService = new UserProfileService(mockClient)
describe('getUserProfile', () => {
it('shoud return the user profile', async () => {
mockClient.query.mockImplementation(() =>
Promise.resolve({ data: mockUserProfile })
)
mockClient.find.mockReturnValue(mockFind)
const result = await userProfileService.getUserProfile()
expect(result).toEqual(mockUserProfile[0])
})
it('shoud return the user profile from string haveSeenOldFluidModal', async () => {
const strMockUserProfile = mockUserProfile
strMockUserProfile[0].haveSeenOldFluidModal =
'2020-11-09T11:27:30.073+01:00'
strMockUserProfile[0].report.monthlyReportDate =
'2020-11-03T00:00:00.000+01:00'
mockClient.query.mockImplementation(() =>
Promise.resolve({ data: strMockUserProfile })
)
mockClient.find.mockReturnValue(mockFind)
const result = await userProfileService.getUserProfile()
expect(result).toEqual(mockUserProfile[0])
})
})
describe('updateUserProfile', () => {
it('shoud return an updated user profile', async () => {
const strMockUserProfile = mockUserProfile
strMockUserProfile[0].haveSeenOldFluidModal =
'2020-11-09T11:27:30.073+01:00'
strMockUserProfile[0].report.monthlyReportDate =
'2020-11-03T00:00:00.000+01:00'
mockClient.query.mockImplementation(() =>
Promise.resolve({ data: strMockUserProfile })
)
mockClient.find.mockReturnValue(mockFind)
mockClient.save.mockResolvedValue({ data: strMockUserProfile[0] })
const result = await userProfileService.updateUserProfile({
haveSeenFavoriteModal: false,
})
const updatedMockUserProfile = mockUserProfile[0]
updatedMockUserProfile.haveSeenFavoriteModal = false
expect(result).toEqual(updatedMockUserProfile)
})
})
})
...@@ -47,10 +47,11 @@ export default class UserProfileService { ...@@ -47,10 +47,11 @@ export default class UserProfileService {
.query(this._client.find(USERPROFILE_DOCTYPE).limitBy(1)) .query(this._client.find(USERPROFILE_DOCTYPE).limitBy(1))
.then(async ({ data }: any) => { .then(async ({ data }: any) => {
const doc = data[0] const doc = data[0]
return await this._client.save({ const saveResult = await this._client.save({
...doc, ...doc,
...attributes, ...attributes,
}) })
return saveResult
}) })
if (userProfile && typeof userProfile.haveSeenOldFluidModal === 'string') { if (userProfile && typeof userProfile.haveSeenOldFluidModal === 'string') {
userProfile.haveSeenOldFluidModal = DateTime.fromISO( userProfile.haveSeenOldFluidModal = DateTime.fromISO(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment