Skip to content

feat: added UT for site #279

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
"prepare": "sf-install",
"test": "wireit",
"test:nuts": "nyc mocha \"**/*.nut.ts\" --slow 4500 --timeout 600000 --parallel",
"test:site": "nyc mocha \"**/site.test.ts\" --slow 4500 --timeout 600000 --parallel",
"test:only": "wireit",
"unlink-lwr": "yarn unlink @lwrjs/api @lwrjs/app-service @lwrjs/asset-registry @lwrjs/asset-transformer @lwrjs/auth-middleware @lwrjs/base-view-provider @lwrjs/base-view-transformer @lwrjs/client-modules @lwrjs/config @lwrjs/core @lwrjs/dev-proxy-server @lwrjs/diagnostics @lwrjs/esbuild @lwrjs/everywhere @lwrjs/fs-asset-provider @lwrjs/fs-watch @lwrjs/html-view-provider @lwrjs/instrumentation @lwrjs/label-module-provider @lwrjs/lambda @lwrjs/legacy-npm-module-provider @lwrjs/loader @lwrjs/lwc-module-provider @lwrjs/lwc-ssr @lwrjs/markdown-view-provider @lwrjs/module-bundler @lwrjs/module-registry @lwrjs/npm-module-provider @lwrjs/nunjucks-view-provider @lwrjs/o11y @lwrjs/resource-registry @lwrjs/router @lwrjs/security @lwrjs/server @lwrjs/shared-utils @lwrjs/static @lwrjs/tools @lwrjs/types @lwrjs/view-registry lwr",
"update-snapshots": "node --loader ts-node/esm --no-warnings=ExperimentalWarning \"./bin/dev.js\" snapshot:generate",
Expand Down
63 changes: 43 additions & 20 deletions test/commands/lightning/dev/site.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,62 @@
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

import { strictEqual } from 'node:assert';
import { TestContext } from '@salesforce/core/testSetup';
// import { expect } from 'chai';
// import { stubSfCommandUx } from '@salesforce/sf-plugins-core';
// import LightningDevSite from '../../../../src/commands/lightning/dev/site.js';
import { expect } from 'chai';
import { Org } from '@salesforce/core';
import { LocalDevOptions } from '@lwrjs/api';
import LightningDevSite from '../../../../src/commands/lightning/dev/site.js';
import { OrgUtils } from '../../../../src/shared/orgUtils.js';

// TODO fix me once we have a fully working command
describe('lightning dev site', () => {
const $$ = new TestContext();
// let sfCommandStubs: ReturnType<typeof stubSfCommandUx>;

beforeEach(() => {
// sfCommandStubs = stubSfCommandUx($$.SANDBOX);
$$.SANDBOX.stub(OrgUtils, 'isLocalDevEnabled').resolves(true);
$$.SANDBOX.stub(OrgUtils, 'ensureMatchingAPIVersion').returns();
});

afterEach(() => {
$$.restore();
});

it('runs hello', async () => {
// await LightningDevSite.run([]);
// const output = sfCommandStubs.log
// .getCalls()
// .flatMap((c) => c.args)
// .join('\n');
// expect(output).to.include('hello world');
it('should have summary, description, and examples defined', () => {
strictEqual(typeof LightningDevSite.summary, 'string', 'Summary should be a string');
strictEqual(typeof LightningDevSite.description, 'string', 'Description should be a string');
strictEqual(typeof LightningDevSite.examples, 'object', 'Examples should be an array');
});

it('result should be undefined if local development is not enabled', async () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably should do something similar to LWC here:
image

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you look at the beforeEach method, they are mocking org data, auth, etc. Thats what you need to get the unit tests running:

beforeEach(async () => {
    stubUx($$.SANDBOX);
    stubSpinner($$.SANDBOX);
    await $$.stubAuths(testOrgData);

    $$.SANDBOX.stub(SfConfig, 'create').withArgs($$.SANDBOX.match.any).resolves(SfConfig.prototype);
    $$.SANDBOX.stub(SfConfig, 'addAllowedProperties').withArgs($$.SANDBOX.match.any);
    $$.SANDBOX.stub(SfConfig.prototype, 'get').returns(undefined);
    $$.SANDBOX.stub(SfConfig.prototype, 'set');
    $$.SANDBOX.stub(SfConfig.prototype, 'write').resolves();
    $$.SANDBOX.stub(Connection.prototype, 'getUsername').returns(testUsername);
    $$.SANDBOX.stub(PreviewUtils, 'getOrCreateAppServerIdentity').resolves(testIdentityData);
    $$.SANDBOX.stub(OrgUtils, 'isLocalDevEnabled').resolves(true);
    $$.SANDBOX.stub(OrgUtils, 'ensureMatchingAPIVersion').returns();

    MockedLightningPreviewApp = await esmock<typeof LightningDevApp>('../../../../src/commands/lightning/dev/app.js', {
      '../../../../src/lwc-dev-server/index.js': {
        startLWCServer: async () => ({ stopServer: () => {} }),
      },
    });
  });

$$.SANDBOX.restore();
$$.SANDBOX.stub(OrgUtils, 'isLocalDevEnabled').resolves(false);
const result = await LightningDevSite.run(['--name', 'Astro', '--target-org', '00Dxx0000001gEH']);
expect(result).to.be.undefined;
});

it('runs hello world --name Astro', async () => {
// await LightningDevSite.run(['--name', 'Astro']);
// const output = sfCommandStubs.log
// .getCalls()
// .flatMap((c) => c.args)
// .join('\n');
// expect(output).to.include('hello Astro');
it('should have valid startupParams', async () => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-explicit-any
const org = new Org({ id: '00Dxx0000001gEH', connection: {} } as any);
$$.SANDBOX.stub(Org, 'create').returns(Promise.resolve(org));

const startupParams: LocalDevOptions = {
sfCLI: true,
authToken: 'test-auth-token',
open: true,
port: 3000,
logLevel: 'error',
mode: 'dev',
siteZip: 'test-site-zip',
siteDir: 'test-site-dir',
};

$$.SANDBOX.stub(LightningDevSite, 'run').resolves(Promise.resolve(startupParams));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test isn't testing anything. You are mocking the entire run method and having it always return startupParams. So you are just testing if(startupParams === startupParams).

process.env.SETUP_ONLY = 'true';

const result = await LightningDevSite.run(['--name', 'Astro', '--target-org', '00Dxx0000001gEH']);
delete process.env.SETUP_ONLY;

expect(result).to.deep.equal(startupParams);
});
});