Newer
Older
import { QueryResult } from 'cozy-client'
import { FLUIDSPRICES_DOCTYPE, PROFILE_DOCTYPE } from 'doctypes'

Hugo SUBTIL
committed
import { FluidPrice, Profile } from 'models'
import mockClient from 'tests/__mocks__/client.mock'
import { mockProfileState } from 'tests/__mocks__/store'
import { migrate, migrationLog } from './migration'
import {
MIGRATION_RESULT_COMPLETE,
MIGRATION_RESULT_FAILED,
MIGRATION_RESULT_NOOP,
} from './migration.data'
import { Migration, MigrationResult } from './migration.type'
describe('migration logger', () => {
const migration: Migration = {
baseSchemaVersion: 0,
targetSchemaVersion: 1,
appVersion: '1.2.4',
description: 'Removing mailToken from profil',
docTypes: PROFILE_DOCTYPE,
run: (mockClient, docs: any[]): Profile[] => {
return docs.map(doc => {
if (doc.mailToken) {
delete doc.mailToken
}
return doc
})
},
}
it('should return noop', () => {
const result: MigrationResult = { type: MIGRATION_RESULT_NOOP, errors: [] }
const reply = migrationLog(migration, result)
expect(reply).toEqual('--- Removing mailToken from profil => NOOP')
})
it('should return Complete', () => {
const result: MigrationResult = {
type: MIGRATION_RESULT_COMPLETE,
errors: [],
}
const reply = migrationLog(migration, result)
expect(reply).toEqual('--- Removing mailToken from profil => Complete')
})
it('should return Failed', () => {
const result: MigrationResult = {
type: MIGRATION_RESULT_FAILED,
errors: [],
}
const reply = migrationLog(migration, result)
expect(reply).toEqual('--- Removing mailToken from profil => Failed')
})
})
describe('migration', () => {
const migration: Migration = {
baseSchemaVersion: 0,
targetSchemaVersion: 1,
appVersion: '1.2.4',
description: 'Removing mailToken from profil',
docTypes: PROFILE_DOCTYPE,
run: (mockClient, docs: any[]): Profile[] => {
return docs.map(doc => {
if (doc.GCUApprovalDate) {
delete doc.GCUApprovalDate
}
return doc
})
},
}
it('should return schema does not exist', async () => {
const mockQueryResult: QueryResult<Schema[]> = {
data: [],
bookmark: '',
next: false,
skip: 0,
}
const mockQueryResult2: QueryResult<Schema[]> = {
data: [{ _id: 'abc', version: 1 }],
bookmark: '',
next: false,
skip: 0,
}
mockClient.query
.mockResolvedValueOnce(mockQueryResult)
.mockResolvedValueOnce(mockQueryResult2)
const result = await migrate(migration, mockClient)
expect(result).toEqual({ type: MIGRATION_RESULT_NOOP, errors: [] })
})
it('should return migration failed', async () => {
const mockQueryResult: QueryResult<Schema[]> = {
data: [],
bookmark: '',
next: false,
skip: 0,
}
const mockQueryResult2: QueryResult<Schema[]> = {
data: [{ _id: 'abc', version: 0 }],
bookmark: '',
next: false,
skip: 0,
}
mockClient.query
.mockResolvedValueOnce(mockQueryResult)
.mockResolvedValueOnce(mockQueryResult2)
const result = await migrate(migration, mockClient)
expect(result).toEqual({
type: MIGRATION_RESULT_FAILED,
errors: [
"TypeError: Cannot read properties of undefined (reading 'data')",
],
it('should return migration complete', async () => {
const mockQueryResultWithoutAnySchema: QueryResult<Schema[]> = {
data: [],
bookmark: '',
next: false,
skip: 0,
}
const mockQueryResultWithCreatedSchema: QueryResult<Schema[]> = {
data: [{ _id: 'abc', version: 0 }],
bookmark: '',
next: false,
skip: 0,
}
const mockQueryResultProfile: QueryResult<Profile[]> = {
bookmark: '',
next: false,
skip: 0,
}
mockClient.query
.mockResolvedValueOnce(mockQueryResultWithoutAnySchema)
.mockResolvedValueOnce(mockQueryResultWithCreatedSchema)
.mockResolvedValueOnce(mockQueryResultProfile)
.mockResolvedValueOnce(mockQueryResultWithCreatedSchema)
const result = await migrate(migration, mockClient)
expect(result).toEqual({
type: MIGRATION_RESULT_COMPLETE,
errors: [],
})
})
it('should return migration noop', async () => {
const migrationTest: Migration = {
baseSchemaVersion: 0,
targetSchemaVersion: 1,
appVersion: '1.2.4',
description: 'Removing mailToken from profil',
docTypes: PROFILE_DOCTYPE,
run: (mockClient, docs: any[]): Profile[] => {
return []
},
}
const mockQueryResultWithoutAnySchema: QueryResult<Schema[]> = {
data: [],
bookmark: '',
next: false,
skip: 0,
}
const mockQueryResultWithCreatedSchema: QueryResult<Schema[]> = {
data: [{ _id: 'abc', version: 0 }],
bookmark: '',
next: false,
skip: 0,
}
const mockQueryResultProfile: QueryResult<Profile[]> = {
bookmark: '',
next: false,
skip: 0,
}
mockClient.query
.mockResolvedValueOnce(mockQueryResultWithoutAnySchema)
.mockResolvedValueOnce(mockQueryResultWithCreatedSchema)
.mockResolvedValueOnce(mockQueryResultProfile)
.mockResolvedValueOnce(mockQueryResultWithCreatedSchema)
const result = await migrate(migrationTest, mockClient)
expect(result).toEqual({
type: MIGRATION_RESULT_NOOP,
errors: [],
})
})
})

Hugo SUBTIL
committed
describe('migration create', () => {
it('should return migration complete for creation', async () => {

Hugo SUBTIL
committed
const migrationTestCreate: Migration = {
baseSchemaVersion: 0,
targetSchemaVersion: 1,
appVersion: '1.2.4',
description: 'Add fluid price',

Hugo SUBTIL
committed
releaseNotes: null,
isCreate: true,

Hugo SUBTIL
committed
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
return []
},
}
const mockCreationDoctypeSchema: QueryResult<Schema[]> = {
data: [{ _id: 'abc', version: 0 }],
bookmark: '',
next: false,
skip: 0,
}
const mockCreationSchema: QueryResult<Schema[]> = {
data: [{ _id: 'abc', version: 0 }],
bookmark: '',
next: false,
skip: 0,
}
const mockCreationDoctype: QueryResult<FluidPrice[]> = {
data: [],
bookmark: '',
next: false,
skip: 0,
}
mockClient.query
.mockResolvedValueOnce(mockCreationDoctypeSchema)
.mockResolvedValueOnce(mockCreationSchema)
.mockResolvedValueOnce(mockCreationDoctype)
.mockResolvedValueOnce(mockCreationSchema)
const result = await migrate(migrationTestCreate, mockClient)
expect(result).toEqual({
type: MIGRATION_RESULT_COMPLETE,
errors: [],
})
})
})