-
Notifications
You must be signed in to change notification settings - Fork 432
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
binoy14
merged 3 commits into
next
from
01-25-test_migration_add_tests_for_normalizeMigrationDefinition
Feb 16, 2024
+103
−0
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
103 changes: 103 additions & 0 deletions
103
packages/@sanity/migrate/src/runner/__tests__/normalizeMigrationDefinition.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', () => { | ||
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') | ||
}) | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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