-
Notifications
You must be signed in to change notification settings - Fork 432
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(core): add tests to getReleaseEditEvents observable
- Loading branch information
1 parent
21db4b8
commit 3f749c7
Showing
8 changed files
with
444 additions
and
96 deletions.
There are no files selected for viewing
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
File renamed without changes.
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
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
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
278 changes: 278 additions & 0 deletions
278
packages/sanity/src/core/releases/tool/detail/activity/getReleaseEditEvents.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,278 @@ | ||
import {type TransactionLogEventWithEffects} from '@sanity/types' | ||
import {TestScheduler} from 'rxjs/testing' | ||
import {type ReleaseDocument, type SanityClient} from 'sanity' | ||
import {afterEach, beforeEach, describe, expect, it, type Mock, vi} from 'vitest' | ||
|
||
import {getTransactionsLogs} from '../../../../store/translog/getTransactionLogs' | ||
import { | ||
EDITS_EVENTS_INITIAL_VALUE, | ||
type getReleaseEditEvents as getReleaseEditEventsFunction, | ||
} from './getReleaseEditEvents' | ||
|
||
const mockClient = { | ||
config: vi.fn().mockReturnValue({dataset: 'testDataset'}), | ||
} as unknown as SanityClient | ||
|
||
vi.mock('../../../../store/translog/getTransactionLogs', () => { | ||
return { | ||
getTransactionsLogs: vi.fn(), | ||
} | ||
}) | ||
const MOCKED_RELEASE = { | ||
userId: '', | ||
_createdAt: '2024-12-05T16:34:59Z', | ||
_rev: 'mocked-rev', | ||
name: 'rWBfpXZVj', | ||
state: 'active', | ||
_updatedAt: '2024-12-05T17:09:28Z', | ||
metadata: { | ||
releaseType: 'undecided', | ||
description: '', | ||
title: 'winter drop', | ||
}, | ||
publishAt: null, | ||
_id: '_.releases.rWBfpXZVj', | ||
_type: 'system.release', | ||
finalDocumentStates: null, | ||
} as unknown as ReleaseDocument | ||
|
||
const MOCKED_TRANSACTION_LOGS: TransactionLogEventWithEffects[] = [ | ||
{ | ||
id: 'mocked-rev', | ||
timestamp: '2024-12-05T17:09:28.325641Z', | ||
author: 'p8xDvUMxC', | ||
documentIDs: ['_.releases.rWBfpXZVj'], | ||
effects: { | ||
'_.releases.rWBfpXZVj': { | ||
apply: [ | ||
11, | ||
3, | ||
23, | ||
0, | ||
12, | ||
22, | ||
'7:09:28', | ||
23, | ||
19, | ||
20, | ||
15, | ||
10, | ||
5, | ||
19, | ||
1, | ||
17, | ||
'undecided', | ||
'releaseType', | ||
15, | ||
], | ||
revert: [ | ||
11, | ||
3, | ||
23, | ||
0, | ||
12, | ||
22, | ||
'6:35:11', | ||
23, | ||
19, | ||
20, | ||
15, | ||
10, | ||
5, | ||
17, | ||
'2024-12-20T16:35:00.000Z', | ||
'intendedPublishAt', | ||
17, | ||
'scheduled', | ||
'releaseType', | ||
15, | ||
], | ||
}, | ||
}, | ||
}, | ||
] | ||
|
||
const MOCKED_EVENT = { | ||
type: 'releaseEditEvent', | ||
author: 'p8xDvUMxC', | ||
change: {releaseType: 'undecided', intendedPublishDate: undefined}, | ||
id: 'mocked-rev', | ||
timestamp: '2024-12-05T17:09:28.325641Z', | ||
releaseName: 'rWBfpXZVj', | ||
} | ||
const mockGetTransactionsLogs = getTransactionsLogs as Mock | ||
|
||
const MOCKED_RELEASES_STATE = { | ||
state: 'loaded' as const, | ||
releaseStack: [], | ||
releases: new Map([[MOCKED_RELEASE._id, MOCKED_RELEASE]]), | ||
} | ||
|
||
describe('getReleaseEditEvents()', () => { | ||
let testScheduler: TestScheduler | ||
let getReleaseEditEvents: typeof getReleaseEditEventsFunction | ||
beforeEach(async () => { | ||
// We need to reset the module and reassign it because it has an internal cache that we need to evict | ||
vi.resetModules() | ||
const testModule = await import('./getReleaseEditEvents') | ||
getReleaseEditEvents = testModule.getReleaseEditEvents | ||
|
||
testScheduler = new TestScheduler((actual, expected) => { | ||
expect(actual).toEqual(expected) | ||
}) | ||
}) | ||
afterEach(() => { | ||
vi.resetAllMocks() | ||
}) | ||
it('should initialize with the default value if releaseId is not provided', () => { | ||
testScheduler.run(({expectObservable, hot}) => { | ||
const releasesState$ = hot('a', {a: MOCKED_RELEASES_STATE}) | ||
const {editEvents$} = getReleaseEditEvents({ | ||
client: mockClient, | ||
releaseId: undefined, | ||
releasesState$: releasesState$, | ||
}) | ||
expectObservable(editEvents$).toBe('(a|)', {a: EDITS_EVENTS_INITIAL_VALUE}) | ||
}) | ||
}) | ||
it('should not get the events if release is undefined', () => { | ||
testScheduler.run(({expectObservable, hot}) => { | ||
const releasesState$ = hot('a', {a: MOCKED_RELEASES_STATE}) | ||
|
||
const {editEvents$} = getReleaseEditEvents({ | ||
client: mockClient, | ||
releaseId: 'not-existing-release', | ||
releasesState$, | ||
}) | ||
|
||
expectObservable(editEvents$).toBe('(a)', {a: EDITS_EVENTS_INITIAL_VALUE}) | ||
}) | ||
}) | ||
it('should get and build the release edit events', () => { | ||
testScheduler.run(({expectObservable, cold, hot}) => { | ||
const releasesState$ = hot('a', {a: MOCKED_RELEASES_STATE}) | ||
|
||
const {editEvents$} = getReleaseEditEvents({ | ||
client: mockClient, | ||
releaseId: MOCKED_RELEASE._id, | ||
releasesState$, | ||
}) | ||
const mockResponse$ = cold('-a|', {a: MOCKED_TRANSACTION_LOGS}) | ||
mockGetTransactionsLogs.mockReturnValueOnce(mockResponse$) | ||
expectObservable(editEvents$).toBe('ab', { | ||
a: {editEvents: [], loading: true}, | ||
b: { | ||
editEvents: [MOCKED_EVENT], | ||
loading: false, | ||
}, | ||
}) | ||
}) | ||
expect(mockGetTransactionsLogs).toHaveBeenCalledWith(mockClient, MOCKED_RELEASE._id, { | ||
effectFormat: 'mendoza', | ||
fromTransaction: undefined, | ||
limit: 100, | ||
reverse: true, | ||
tag: 'sanity.studio.release.history', | ||
toTransaction: MOCKED_RELEASE._rev, | ||
}) | ||
}) | ||
it('should not refetch the edit events if rev has not changed', () => { | ||
testScheduler.run(({expectObservable, cold, hot}) => { | ||
// Simulate the release states changing over time, but the _rev is the same | ||
// 'a' at frame 0: initial state with _rev=rev1 | ||
// 'b' at frame 5: updated state with _rev=rev2 | ||
const releasesState$ = hot('a---b', { | ||
a: MOCKED_RELEASES_STATE, | ||
b: MOCKED_RELEASES_STATE, | ||
}) | ||
const {editEvents$} = getReleaseEditEvents({ | ||
client: mockClient, | ||
releaseId: MOCKED_RELEASE._id, | ||
releasesState$: releasesState$, | ||
}) | ||
const mockResponse$ = cold('-a|', {a: MOCKED_TRANSACTION_LOGS}) | ||
mockGetTransactionsLogs.mockReturnValueOnce(mockResponse$) | ||
// Even though the state changes, the editEvents$ should not emit again | ||
expectObservable(editEvents$).toBe('ab', { | ||
a: {editEvents: [], loading: true}, | ||
b: { | ||
editEvents: [MOCKED_EVENT], | ||
loading: false, | ||
}, | ||
}) | ||
}) | ||
expect(mockGetTransactionsLogs).toHaveBeenCalledWith(mockClient, MOCKED_RELEASE._id, { | ||
effectFormat: 'mendoza', | ||
fromTransaction: undefined, | ||
limit: 100, | ||
reverse: true, | ||
tag: 'sanity.studio.release.history', | ||
toTransaction: MOCKED_RELEASE._rev, | ||
}) | ||
}) | ||
it('should refetch the edit events if release._rev changes', () => { | ||
testScheduler.run(({expectObservable, cold, hot}) => { | ||
// Define the initial and updated release state | ||
const updatedReleaseState = { | ||
...MOCKED_RELEASES_STATE, | ||
releases: new Map([[MOCKED_RELEASE._id, {...MOCKED_RELEASE, _rev: 'changed-rev'}]]), | ||
} | ||
// Simulate the release states changing over time | ||
// 'a' at frame 0: initial state with _rev=rev1 | ||
// 'b' at frame 5: updated state with _rev=rev2 | ||
const releasesState$ = hot('a---b', { | ||
a: MOCKED_RELEASES_STATE, | ||
b: updatedReleaseState, | ||
}) | ||
|
||
const {editEvents$} = getReleaseEditEvents({ | ||
client: mockClient, | ||
releaseId: MOCKED_RELEASE._id, | ||
releasesState$: releasesState$, | ||
}) | ||
|
||
const mockResponse$ = cold('-a|', {a: MOCKED_TRANSACTION_LOGS}) | ||
const newTransaction = { | ||
id: 'changed-rev', | ||
timestamp: '2024-12-05T17:10:28.325641Z', | ||
author: 'p8xDvUMxC', | ||
documentIDs: ['_.releases.rWBfpXZVj'], | ||
effects: {}, | ||
} | ||
// It only returns the new transactions, the rest are from the cache, so they will be persisted. | ||
const mockResponse2$ = cold('-a|', {a: [newTransaction]}) | ||
|
||
mockGetTransactionsLogs.mockReturnValueOnce(mockResponse$).mockReturnValueOnce(mockResponse2$) | ||
|
||
expectObservable(editEvents$).toBe('ab---c', { | ||
a: {editEvents: [], loading: true}, | ||
b: { | ||
editEvents: [MOCKED_EVENT], | ||
loading: false, | ||
}, | ||
c: { | ||
editEvents: [MOCKED_EVENT], | ||
loading: false, | ||
}, | ||
}) | ||
}) | ||
expect(mockGetTransactionsLogs).toHaveBeenCalledWith(mockClient, MOCKED_RELEASE._id, { | ||
effectFormat: 'mendoza', | ||
fromTransaction: undefined, | ||
limit: 100, | ||
reverse: true, | ||
tag: 'sanity.studio.release.history', | ||
toTransaction: MOCKED_RELEASE._rev, | ||
}) | ||
expect(mockGetTransactionsLogs).toHaveBeenCalledWith(mockClient, MOCKED_RELEASE._id, { | ||
effectFormat: 'mendoza', | ||
// Uses the previous release._rev as the fromTransaction | ||
fromTransaction: MOCKED_RELEASE._rev, | ||
limit: 100, | ||
reverse: true, | ||
tag: 'sanity.studio.release.history', | ||
// Uses the new release._rev as the toTransaction | ||
toTransaction: 'changed-rev', | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.