Skip to content
Snippets Groups Projects
migration.spec.ts 6.83 KiB
Newer Older
  • Learn to ignore specific revisions
  • import { QueryResult } from 'cozy-client'
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    import { FLUIDSPRICES_DOCTYPE, PROFILE_DOCTYPE } from 'doctypes'
    
    import { FluidPrice, Profile } from 'models'
    
    import { Schema } from 'models/schema.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,
    
    } 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,
    
        releaseNotes: null,
    
        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,
    
        releaseNotes: null,
    
        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[]> = {
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
          data: [mockProfileState],
    
          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,
    
          releaseNotes: null,
    
          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[]> = {
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
          data: [mockProfileState],
    
          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: [],
        })
      })
    })
    
      it('should return migration complete for creation', async () => {
    
        const migrationTestCreate: Migration = {
          baseSchemaVersion: 0,
          targetSchemaVersion: 1,
          appVersion: '1.2.4',
          description: 'Add fluid price',
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
          docTypes: FLUIDSPRICES_DOCTYPE,
    
          run: (): FluidPrice[] => {
    
            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: [],
        })
      })
    })