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

test(migration) add tests for normalizeMigrationDefinition #5565

Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import {
createAsyncIterableMutation,
normalizeMigrateDefinition,
} from '../normalizeMigrateDefinition'
import {Migration, NodeMigration} from '../../types'
import {createIfNotExists} from '../../mutations'

const mockAsyncIterableIterator = () => {
const data = [{_id: 'mockId', _type: 'mockDocumentType'}]
let index = 0

return {
next: jest.fn().mockImplementation(() => {
if (index < data.length) {
return Promise.resolve({value: data[index++], done: false})
}
return Promise.resolve({value: undefined, done: true})
}),
[Symbol.asyncIterator]: jest.fn().mockImplementation(function (this: unknown) {
return this
}),
}
}

describe('#normalizeMigrateDefinition', () => {
it('should return the migrate is a function', async () => {
const mockMigration: Migration = {
title: 'mockMigration',
documentTypes: ['mockDocumentType'],
async *migrate() {
yield createIfNotExists({_type: 'mockDocumentType', _id: 'mockId'})
},
}

const result = normalizeMigrateDefinition(mockMigration)

const res = []
for await (const item of result(jest.fn(), {} as any)) {
res.push(item)
}

expect(res.flat()).toEqual([createIfNotExists({_type: 'mockDocumentType', _id: 'mockId'})])
})

it('should return a new mutations if migrate is not a function', async () => {
const mockMigration: Migration = {
title: 'mockMigration',
documentTypes: ['mockDocumentType'],
migrate: {
document() {
return createIfNotExists({_type: 'mockDocumentType', _id: 'mockId'})
},
},
}

const result = normalizeMigrateDefinition(mockMigration)
const res = []

for await (const item of result(mockAsyncIterableIterator, {} as any)) {
res.push(item)
}

expect(res.flat()).toEqual([createIfNotExists({_type: 'mockDocumentType', _id: 'mockId'})])
})

it('should not return undefined if migrate is returning undefined', async () => {
const mockMigration: Migration = {
title: 'mockMigration',
documentTypes: ['mockDocumentType'],
migrate: {
document() {
return undefined
},
},
}

const result = normalizeMigrateDefinition(mockMigration)
const res = []

for await (const item of result(mockAsyncIterableIterator, {} as any)) {
res.push(item)
}

expect(res.flat()).toEqual([])
})
})

describe('#createAsyncIterableMutation', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this test just testing that we follow an interface?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for the most part it just ensures that it is returns an iterable. normalizeMigrateDefinition internally calls this function so the above tests do actually test most of it's functionality

it('should return an async iterable', async () => {
const mockMigration: NodeMigration = {
document: jest.fn(),
}

const iterable = createAsyncIterableMutation(mockMigration, {documentTypes: ['foo']})

expect(typeof iterable).toBe('function')

const iterator = iterable(mockAsyncIterableIterator() as any, {} as any)
expect(typeof iterator.next).toBe('function')
expect(typeof iterator.return).toBe('function')
expect(typeof iterator.throw).toBe('function')
})
})
Loading