refactor: avoid deallocing note controller until local propagation complete

This commit is contained in:
Mo
2022-12-05 07:53:21 -06:00
parent 46b19d8caf
commit bd5ccbfab6
2 changed files with 59 additions and 8 deletions

View File

@@ -7,6 +7,8 @@ import {
SNTag,
ItemsClientInterface,
SNNote,
Deferred,
SyncServiceInterface,
} from '@standardnotes/snjs'
import { FeatureIdentifier, NoteType } from '@standardnotes/features'
import { NoteViewController } from './NoteViewController'
@@ -17,10 +19,16 @@ describe('note view controller', () => {
beforeEach(() => {
application = {} as jest.Mocked<WebApplication>
application.streamItems = jest.fn()
application.streamItems = jest.fn().mockReturnValue(() => {})
application.getPreference = jest.fn().mockReturnValue(true)
application.noAccount = jest.fn().mockReturnValue(false)
application.isNativeMobileWeb = jest.fn().mockReturnValue(false)
Object.defineProperty(application, 'items', { value: {} as jest.Mocked<ItemsClientInterface> })
Object.defineProperty(application, 'sync', { value: {} as jest.Mocked<SyncServiceInterface> })
application.sync.sync = jest.fn().mockReturnValue(Promise.resolve())
componentManager = {} as jest.Mocked<SNComponentManager>
componentManager.legacyGetDefaultEditor = jest.fn()
Object.defineProperty(application, 'componentManager', { value: componentManager })
@@ -80,4 +88,30 @@ describe('note view controller', () => {
expect(controller['defaultTag']).toEqual(tag)
expect(application.items.addTagToNote).toHaveBeenCalledWith(expect.anything(), tag, expect.anything())
})
it('should wait until item finishes saving locally before deiniting', async () => {
const note = {
uuid: 'note-uuid',
} as jest.Mocked<SNNote>
application.items.findItem = jest.fn().mockReturnValue(note)
const controller = new NoteViewController(application, note)
await controller.initialize()
const changePromise = Deferred()
application.mutator.changeItem = jest.fn().mockReturnValue(changePromise.promise)
const savePromise = controller.saveAndAwaitLocalPropagation({ isUserModified: true, bypassDebouncer: true })
controller.deinit()
expect(controller.dealloced).toEqual(false)
changePromise.resolve(true)
await changePromise.promise
await savePromise
expect(controller.dealloced).toEqual(true)
})
})