Skip to content

Commit

Permalink
test that validation is called after the migrations finish
Browse files Browse the repository at this point in the history
  • Loading branch information
owencraston committed Jan 23, 2025
1 parent cd699c1 commit ac67868
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions app/store/migrations/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,37 @@ describe('asyncifyMigrations', () => {

expect(isPromiseMigrations).toEqual(true);
});

it('should only call validation callback after all migrations complete', async () => {
const mockValidation = jest.fn();
const testMigrationList = {
0: synchronousMigration,
1: asyncMigration,
2: synchronousMigration,
};

// Convert all migrations to async with validation callback
const asyncMigrations = asyncifyMigrations(
testMigrationList,
mockValidation,
);

// Run migrations in sequence and verify validation is only called after the highest migration
let state: PersistedState = initialState;

for (const migrationKey in asyncMigrations) {
state = (await asyncMigrations[migrationKey](state)) as PersistedState;

if (Number(migrationKey) === 2) {
// Should be called exactly once after the last migration
expect(mockValidation).toHaveBeenCalledTimes(1);
expect(mockValidation).toHaveBeenCalledWith(state);
} else {
// Should not be called for any other migration
expect(mockValidation).not.toHaveBeenCalled();
}
}
});
});

describe('migrations', () => {
Expand Down

0 comments on commit ac67868

Please sign in to comment.