From 8e74f7094c1f0e7b529dde9298982edbb0c3a172 Mon Sep 17 00:00:00 2001 From: Blake Stovall Date: Mon, 2 Dec 2024 11:14:25 -0600 Subject: [PATCH] Fix push queue tests (#2661) --- src/redux-middleware/__tests__/pushQueue.ts | 141 ++++++++------------ 1 file changed, 55 insertions(+), 86 deletions(-) diff --git a/src/redux-middleware/__tests__/pushQueue.ts b/src/redux-middleware/__tests__/pushQueue.ts index 0502e292c28..1692b5ad1aa 100644 --- a/src/redux-middleware/__tests__/pushQueue.ts +++ b/src/redux-middleware/__tests__/pushQueue.ts @@ -1,93 +1,62 @@ -// import _ from 'lodash' -// import SimplePath from '../../@types/SimplePath' -// import deleteThought from '../../actions/deleteThought' -// import editThought from '../../actions/editThought' -// import importText from '../../actions/importText' -// import { HOME_PATH, HOME_TOKEN } from '../../constants' -// import db from '../../data-providers/yjs' -// import contextToPath from '../../selectors/contextToPath' -// import store from '../../stores/app' -// import contextToThought from '../../test-helpers/contextToThought' -import createTestApp, { cleanupTestApp } from '../../test-helpers/createTestApp' +import { act } from 'react' +import { importTextActionCreator as importText } from '../../actions/importText' +import getLexemeFromProvider from '../../data-providers/data-helpers/getLexeme' +import db from '../../data-providers/yjs/thoughtspace' +import getLexemeFromState from '../../selectors/getLexeme' +import store from '../../stores/app' +import contextToThought from '../../test-helpers/contextToThought' +import createTestApp, { cleanupTestApp, refreshTestApp } from '../../test-helpers/createTestApp' +import dispatch from '../../test-helpers/dispatch' +import { editThoughtByContextActionCreator as editThought } from '../../test-helpers/editThoughtByContext' -// import testTimer from '../../test-helpers/testTimer' +beforeEach(createTestApp) +afterEach(cleanupTestApp) -// import head from '../../util/head' -// import parentOf from '../../util/parentOf' +// Current functionality is broken in main and won't be fixed soon so this test is skipped. +it.skip('editing a thought should load the lexeme and merge contexts', async () => { + // Related issue: https://github.com/cybersemics/em/issues/1074 + await dispatch( + importText({ + text: ` + - g + - h + - a + - b + - c + - d + - e + - f`, + }), + ) -/* - Note: sinon js fake timer is used to overcome some short comming we have with jest's fake timer. - For details: https://github.com/cybersemics/em/issues/919#issuecomment-739135971 -*/ + await act(vi.runOnlyPendingTimersAsync) -// const fakeTimer = testTimer() + expect((await getLexemeFromProvider(db, 'f'))?.contexts).toHaveLength(1) -beforeEach(createTestApp) -afterEach(cleanupTestApp) + const thoughtH = contextToThought(store.getState(), ['g', 'h']) + const thoughtF = contextToThought(store.getState(), ['a', 'b', 'c', 'd', 'e', 'f']) -// TODO: test stopped working with yjs -// currently all lexemes are loaded in memory -it.skip('editing a thought should load the lexeme and merge contexts', async () => { - // // Related issue: https://github.com/cybersemics/em/issues/1074 - // - // fakeTimer.useFakeTimer() - // - // store.dispatch( - // importText({ - // text: ` - // - g - // - h - // - a - // - b - // - c - // - d - // - e - // - f`, - // }), - // ) - // - // await fakeTimer.runAllAsync() - // - // await fakeTimer.useRealTimer() - // - // expect((await getLexemeDb(db, 'f'))?.contexts).toHaveLength(1) - // - // const thoughtH = contextToThought(store.getState(), ['g', 'h']) - // const thoughtF = contextToThought(store.getState(), ['a', 'b', 'c', 'd', 'e', 'f']) - // - // await refreshTestApp() - // - // fakeTimer.useFakeTimer() - // - // // lexeme for 'f' should not be loaded into the state yet. - // expect(getLexemeState(store.getState(), 'f')).toBeFalsy() - // - // const pathGH = contextToPath(store.getState(), ['g', 'h']) as SimplePath - // - // store.dispatch( - // editThought({ - // oldValue: 'h', - // newValue: 'f', - // path: pathGH, - // }), - // ) - // await fakeTimer.runAllAsync() - // - // fakeTimer.useRealTimer() - // - // // existing Lexemes should be pulled and synced after thought is edited. - // - // // both db and state should have same updated lexeme - // const thoughtContextsState = getLexemeState(store.getState(), 'f')?.contexts - // - // // Note: Thought h has been changed to f but the id remains the same - // // check that state has the correct contexts, ignoring order and ids - // expect(thoughtContextsState).toEqual(expect.arrayContaining([thoughtH?.id, thoughtF?.id])) - // expect(thoughtContextsState).toHaveLength(2) - // - // // check that db has the correct contexts, ignoring order and ids - // const thoughtContextsDb = (await getLexemeDb(db, 'f'))?.contexts - // expect(thoughtContextsDb).toEqual(expect.arrayContaining([thoughtH?.id, thoughtF?.id])) - // - // expect(thoughtContextsState).toHaveLength(2) + await refreshTestApp() + // lexeme for 'f' should not be loaded into the state yet. + expect(getLexemeFromState(store.getState(), 'f')).toBeFalsy() + + await dispatch(editThought(['g', 'h'], 'f')) + + await act(vi.runAllTimersAsync) + + // existing Lexemes should be pulled and synced after thought is edited. + + // both db and state should have same updated lexeme + const thoughtContextsState = getLexemeFromState(store.getState(), 'f')?.contexts + + // Note: Thought h has been changed to f but the id remains the same + // check that state has the correct contexts, ignoring order and ids + expect(thoughtContextsState).toEqual(expect.arrayContaining([thoughtH?.id, thoughtF?.id])) + expect(thoughtContextsState).toHaveLength(2) + + // check that db has the correct contexts, ignoring order and ids + const thoughtContextsDb = (await getLexemeFromProvider(db, 'f'))?.contexts + expect(thoughtContextsDb).toEqual(expect.arrayContaining([thoughtH?.id, thoughtF?.id])) + + expect(thoughtContextsState).toHaveLength(2) })