|
| 1 | +/*! |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | +import * as vscode from 'vscode' |
| 6 | +import * as assert from 'assert' |
| 7 | +import { FakeExtensionContext } from '../fakeExtensionContext' |
| 8 | +import { ExtensionUse } from '../../auth/utils' |
| 9 | + |
| 10 | +describe('ExtensionUse.isFirstUse()', function () { |
| 11 | + let fakeState: vscode.Memento |
| 12 | + let instance: ExtensionUse |
| 13 | + |
| 14 | + beforeEach(async function () { |
| 15 | + fakeState = (await FakeExtensionContext.create()).globalState |
| 16 | + instance = new ExtensionUse() |
| 17 | + fakeState.update(ExtensionUse.instance.isExtensionFirstUseKey, true) |
| 18 | + }) |
| 19 | + |
| 20 | + it('is true only on first startup', function () { |
| 21 | + assert.strictEqual(instance.isFirstUse(fakeState), true, 'Failed on first call.') |
| 22 | + assert.strictEqual(instance.isFirstUse(fakeState), true, 'Failed on second call.') |
| 23 | + |
| 24 | + const nextStartup = nextExtensionStartup() |
| 25 | + assert.strictEqual(nextStartup.isFirstUse(fakeState), false, 'Failed on new startup.') |
| 26 | + }) |
| 27 | + |
| 28 | + it('true when: (state value not exists + NOT has existing connections)', async function () { |
| 29 | + await makeStateValueNotExist() |
| 30 | + const notHasExistingConnections = () => false |
| 31 | + assert.strictEqual( |
| 32 | + instance.isFirstUse(fakeState, notHasExistingConnections), |
| 33 | + true, |
| 34 | + 'No existing connections, should be first use' |
| 35 | + ) |
| 36 | + assert.strictEqual(nextExtensionStartup().isFirstUse(fakeState), false) |
| 37 | + }) |
| 38 | + |
| 39 | + it('false when: (state value not exists + has existing connections)', async function () { |
| 40 | + await makeStateValueNotExist() |
| 41 | + const hasExistingConnections = () => true |
| 42 | + assert.strictEqual( |
| 43 | + instance.isFirstUse(fakeState, hasExistingConnections), |
| 44 | + false, |
| 45 | + 'Found existing connections, should not be first use' |
| 46 | + ) |
| 47 | + assert.strictEqual(nextExtensionStartup().isFirstUse(fakeState), false) |
| 48 | + }) |
| 49 | + |
| 50 | + /** |
| 51 | + * This makes the backend state value: undefined, mimicking a brand new user. |
| 52 | + * We use this state value to track if user is a first time user. |
| 53 | + */ |
| 54 | + async function makeStateValueNotExist() { |
| 55 | + await fakeState.update(ExtensionUse.instance.isExtensionFirstUseKey, undefined) |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Mimics when the extension startsup a subsequent time (i.e user closes vscode and opens again). |
| 60 | + */ |
| 61 | + function nextExtensionStartup() { |
| 62 | + return new ExtensionUse() |
| 63 | + } |
| 64 | +}) |
0 commit comments