tests: fix memory leaks (#2389)

This commit is contained in:
Mo
2023-08-06 15:23:31 -05:00
committed by GitHub
parent d59e1befff
commit 8655bdb5dd
76 changed files with 3904 additions and 3840 deletions

View File

@@ -1,7 +1,6 @@
/* eslint-disable no-unused-expressions */
/* eslint-disable no-undef */
import { BaseItemCounts } from '../lib/BaseItemCounts.js'
import * as Factory from '../lib/factory.js'
chai.use(chaiAsPromised)
const expect = chai.expect
@@ -11,47 +10,52 @@ describe('items', () => {
awaitAll: true,
}
let application
let expectedItemCount
beforeEach(async function () {
this.expectedItemCount = BaseItemCounts.DefaultItems
this.application = await Factory.createInitAppWithFakeCrypto()
localStorage.clear()
expectedItemCount = BaseItemCounts.DefaultItems
application = await Factory.createInitAppWithFakeCrypto()
})
afterEach(async function () {
await Factory.safeDeinit(this.application)
await Factory.safeDeinit(application)
localStorage.clear()
})
it('setting an item as dirty should update its client updated at', async function () {
const params = Factory.createNotePayload()
await this.application.mutator.emitItemsFromPayloads([params], PayloadEmitSource.LocalChanged)
const item = this.application.items.items[0]
await application.mutator.emitItemsFromPayloads([params], PayloadEmitSource.LocalChanged)
const item = application.items.items[0]
const prevDate = item.userModifiedDate.getTime()
await Factory.sleep(0.1)
await this.application.mutator.setItemDirty(item, true)
const refreshedItem = this.application.items.findItem(item.uuid)
await application.mutator.setItemDirty(item, true)
const refreshedItem = application.items.findItem(item.uuid)
const newDate = refreshedItem.userModifiedDate.getTime()
expect(prevDate).to.not.equal(newDate)
})
it('setting an item as dirty with option to skip client updated at', async function () {
const params = Factory.createNotePayload()
await this.application.mutator.emitItemsFromPayloads([params], PayloadEmitSource.LocalChanged)
const item = this.application.items.items[0]
await application.mutator.emitItemsFromPayloads([params], PayloadEmitSource.LocalChanged)
const item = application.items.items[0]
const prevDate = item.userModifiedDate.getTime()
await Factory.sleep(0.1)
await this.application.mutator.setItemDirty(item)
await application.mutator.setItemDirty(item)
const newDate = item.userModifiedDate.getTime()
expect(prevDate).to.equal(newDate)
})
it('properly pins, archives, and locks', async function () {
const params = Factory.createNotePayload()
await this.application.mutator.emitItemsFromPayloads([params], PayloadEmitSource.LocalChanged)
await application.mutator.emitItemsFromPayloads([params], PayloadEmitSource.LocalChanged)
const item = this.application.items.items[0]
const item = application.items.items[0]
expect(item.pinned).to.not.be.ok
const refreshedItem = (
await this.application.changeAndSaveItem.execute(
await application.changeAndSaveItem.execute(
item,
(mutator) => {
mutator.pinned = true
@@ -71,16 +75,16 @@ describe('items', () => {
it('properly compares item equality', async function () {
const params1 = Factory.createNotePayload()
const params2 = Factory.createNotePayload()
await this.application.mutator.emitItemsFromPayloads([params1, params2], PayloadEmitSource.LocalChanged)
await application.mutator.emitItemsFromPayloads([params1, params2], PayloadEmitSource.LocalChanged)
let item1 = this.application.items.getDisplayableNotes()[0]
let item2 = this.application.items.getDisplayableNotes()[1]
let item1 = application.items.getDisplayableNotes()[0]
let item2 = application.items.getDisplayableNotes()[1]
expect(item1.isItemContentEqualWith(item2)).to.equal(true)
// items should ignore this field when checking for equality
item1 = (
await this.application.changeAndSaveItem.execute(
await application.changeAndSaveItem.execute(
item1,
(mutator) => {
mutator.userModifiedDate = new Date()
@@ -91,7 +95,7 @@ describe('items', () => {
)
).getValue()
item2 = (
await this.application.changeAndSaveItem.execute(
await application.changeAndSaveItem.execute(
item2,
(mutator) => {
mutator.userModifiedDate = undefined
@@ -105,7 +109,7 @@ describe('items', () => {
expect(item1.isItemContentEqualWith(item2)).to.equal(true)
item1 = (
await this.application.changeAndSaveItem.execute(
await application.changeAndSaveItem.execute(
item1,
(mutator) => {
mutator.mutableContent.foo = 'bar'
@@ -119,7 +123,7 @@ describe('items', () => {
expect(item1.isItemContentEqualWith(item2)).to.equal(false)
item2 = (
await this.application.changeAndSaveItem.execute(
await application.changeAndSaveItem.execute(
item2,
(mutator) => {
mutator.mutableContent.foo = 'bar'
@@ -134,7 +138,7 @@ describe('items', () => {
expect(item2.isItemContentEqualWith(item1)).to.equal(true)
item1 = (
await this.application.changeAndSaveItem.execute(
await application.changeAndSaveItem.execute(
item1,
(mutator) => {
mutator.e2ePendingRefactor_addItemAsRelationship(item2)
@@ -145,7 +149,7 @@ describe('items', () => {
)
).getValue()
item2 = (
await this.application.changeAndSaveItem.execute(
await application.changeAndSaveItem.execute(
item2,
(mutator) => {
mutator.e2ePendingRefactor_addItemAsRelationship(item1)
@@ -162,7 +166,7 @@ describe('items', () => {
expect(item1.isItemContentEqualWith(item2)).to.equal(false)
item1 = (
await this.application.changeAndSaveItem.execute(
await application.changeAndSaveItem.execute(
item1,
(mutator) => {
mutator.removeItemAsRelationship(item2)
@@ -173,7 +177,7 @@ describe('items', () => {
)
).getValue()
item2 = (
await this.application.changeAndSaveItem.execute(
await application.changeAndSaveItem.execute(
item2,
(mutator) => {
mutator.removeItemAsRelationship(item1)
@@ -192,13 +196,13 @@ describe('items', () => {
it('content equality should not have side effects', async function () {
const params1 = Factory.createNotePayload()
const params2 = Factory.createNotePayload()
await this.application.mutator.emitItemsFromPayloads([params1, params2], PayloadEmitSource.LocalChanged)
await application.mutator.emitItemsFromPayloads([params1, params2], PayloadEmitSource.LocalChanged)
let item1 = this.application.items.getDisplayableNotes()[0]
const item2 = this.application.items.getDisplayableNotes()[1]
let item1 = application.items.getDisplayableNotes()[0]
const item2 = application.items.getDisplayableNotes()[1]
item1 = (
await this.application.changeAndSaveItem.execute(
await application.changeAndSaveItem.execute(
item1,
(mutator) => {
mutator.mutableContent.foo = 'bar'
@@ -223,7 +227,7 @@ describe('items', () => {
// There was an issue where calling that function would modify values directly to omit keys
// in contentKeysToIgnoreWhenCheckingEquality.
await this.application.mutator.setItemsDirty([item1, item2])
await application.mutator.setItemsDirty([item1, item2])
expect(item1.userModifiedDate).to.be.ok
expect(item2.userModifiedDate).to.be.ok