From 2f07f7a3d105ad09db092d13c45ea86662b51fa3 Mon Sep 17 00:00:00 2001 From: moughxyz Date: Mon, 16 Jan 2023 09:53:39 -0600 Subject: [PATCH] test: refactor flakey test to use app context --- packages/snjs/mocha/application.test.js | 36 +++++++++++-------------- packages/snjs/mocha/lib/AppContext.js | 30 ++++++--------------- packages/snjs/mocha/lib/factory.js | 4 +-- 3 files changed, 25 insertions(+), 45 deletions(-) diff --git a/packages/snjs/mocha/application.test.js b/packages/snjs/mocha/application.test.js index b3f5af9e5..16c468df8 100644 --- a/packages/snjs/mocha/application.test.js +++ b/packages/snjs/mocha/application.test.js @@ -64,31 +64,25 @@ describe('application instances', () => { }) it('changing default host should not affect already signed in accounts', async () => { - /** This test will always succeed but should be observed for console exceptions */ - const app = await Factory.createAndInitializeApplication( - 'app', - Environment.Web, - Platform.MacWeb, - Factory.getDefaultHost(), - ) - await Factory.registerUserToApplication({ - application: app, - email: UuidGenerator.GenerateUuid(), - password: 'password', + const contextA = await Factory.createAppContext({ + identifier: 'app', + host: Factory.getDefaultHost(), }) - await app.prepareForDeinit() - await Factory.safeDeinit(app) + await contextA.launch() + await contextA.register() + await contextA.deinit() /** Recreate app with different host */ - const recreatedApp = await Factory.createAndInitializeApplication( - 'app', - Environment.Web, - Platform.MacWeb, - 'http://nonsense.host', - ) + const recreatedContext = await Factory.createAppContext({ + identifier: 'app', + host: 'http://nonsense.host' + }) + await recreatedContext.launch() - expect(recreatedApp.getHost()).to.not.equal('http://nonsense.host') - expect(recreatedApp.getHost()).to.equal(Factory.getDefaultHost()) + expect(recreatedContext.application.getHost()).to.not.equal('http://nonsense.host') + expect(recreatedContext.application.getHost()).to.equal(Factory.getDefaultHost()) + + await recreatedContext.deinit() }) it('signing out application should delete snjs_version', async () => { diff --git a/packages/snjs/mocha/lib/AppContext.js b/packages/snjs/mocha/lib/AppContext.js index 5c7d571be..d1107d3cd 100644 --- a/packages/snjs/mocha/lib/AppContext.js +++ b/packages/snjs/mocha/lib/AppContext.js @@ -1,6 +1,7 @@ import FakeWebCrypto from './fake_web_crypto.js' import * as Applications from './Applications.js' import * as Utils from './Utils.js' +import * as Defaults from './Defaults.js' import { createNotePayload } from './Items.js' UuidGenerator.SetGenerator(new FakeWebCrypto().generateUUID) @@ -11,28 +12,13 @@ const MaximumSyncOptions = { } export class AppContext { - constructor({ identifier, crypto, email, password, passcode } = {}) { - if (!identifier) { - identifier = `${Math.random()}` - } - - if (!email) { - email = UuidGenerator.GenerateUuid() - } - - if (!password) { - password = UuidGenerator.GenerateUuid() - } - - if (!passcode) { - passcode = 'mypasscode' - } - - this.identifier = identifier + constructor({ identifier, crypto, email, password, passcode, host } = {}) { + this.identifier = identifier || `${Math.random()}` this.crypto = crypto - this.email = email - this.password = password - this.passcode = passcode + this.email = email || UuidGenerator.GenerateUuid() + this.password = password || UuidGenerator.GenerateUuid() + this.passcode = passcode || 'mypasscode' + this.host = host || Defaults.getDefaultHost() } enableLogging() { @@ -55,7 +41,7 @@ export class AppContext { this.identifier, undefined, undefined, - undefined, + this.host, this.crypto || new FakeWebCrypto(), ) } diff --git a/packages/snjs/mocha/lib/factory.js b/packages/snjs/mocha/lib/factory.js index d2cc61e82..ef46abff7 100644 --- a/packages/snjs/mocha/lib/factory.js +++ b/packages/snjs/mocha/lib/factory.js @@ -51,8 +51,8 @@ export async function createAppContextWithRealCrypto(identifier) { return createAppContext({ identifier, crypto: new SNWebCrypto() }) } -export async function createAppContext({ identifier, crypto, email, password } = {}) { - const context = new AppContext({ identifier, crypto, email, password }) +export async function createAppContext({ identifier, crypto, email, password, host } = {}) { + const context = new AppContext({ identifier, crypto, email, password, host }) await context.initialize() return context }