Skip to content

Commit

Permalink
test: ut
Browse files Browse the repository at this point in the history
  • Loading branch information
mshanemc committed Aug 16, 2023
1 parent e0736e8 commit c7bed7c
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 51 deletions.
1 change: 1 addition & 0 deletions .mocharc.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"require": "ts-node/register,source-map-support/register",
"watch-extensions": "ts",
"watch-files": ["src/**/*.ts", "test/**/*.ts"],
"recursive": true,
"reporter": "spec",
"timeout": 600000
Expand Down
79 changes: 79 additions & 0 deletions test/commands/discover.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright (c) 2023, salesforce.com, inc.
* All rights reserved.
* 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 { TestContext } from '@salesforce/core/lib/testSetup';
import { expect, config } from 'chai';
import { stubSfCommandUx } from '@salesforce/sf-plugins-core';
import Discover from '../../src/commands/plugins/discover';
import * as queryStubs from '../../src/shared/discoverQuery';

const lorem = `Lorem ipsum dolor sit amet, consectetur adipiscing elit. ============================= Phasellus a magna odio. Nullam bibendum magna a lectus faucibus, et viverra neque ornare. Vestibulum tellus augue, rutrum et semper id, pellentesque eget lacus. Nunc ac ultricies nulla, in sagittis arcu. Maecenas accumsan ac dui eget pharetra. Interdum et malesuada fames ac ante ipsum primis in faucibus. Quisque ac nisi pretium, suscipit metus ac, sodales nisl. Quisque eget finibus nunc, id iaculis quam. Fusce a ultrices leo. Morbi eu est condimentum sapien semper porttitor non ac nisl. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.
Praesent viverra interdum enim commodo ullamcorper. Suspendisse potenti. Suspendisse tristique dignissim vestibulum. Maecenas non velit faucibus, dapibus tellus sit amet, hendrerit lacus. Nullam metus nunc, suscipit venenatis tempor eu, facilisis pulvinar tellus. Proin venenatis pretium justo, at venenatis sapien pharetra porttitor. Cras egestas eget mi tristique ullamcorper. Etiam pretium non risus quis maximus. Fusce in pulvinar ipsum, in elementum magna. Donec nec lacus sit amet est fringilla vehicula sit amet ac ante. Fusce non laoreet arcu. Sed nisi dolor, tempus nec tortor non, placerat tincidunt ligula.
`;

config.truncateThreshold = 0;
describe('plugins discover', () => {
const $$ = new TestContext();
let sfCommandStubs: ReturnType<typeof stubSfCommandUx>;

beforeEach(() => {
sfCommandStubs = stubSfCommandUx($$.SANDBOX);
$$.SANDBOX.stub(queryStubs, 'query').resolves([
[
{ name: '@salesforce/foo', description: 'FooDesc', homepage: 'https://www.salesforce.com' },
{ downloads: '10' },
{
objects: [
{ package: { name: '@salesforce/bar', date: '2021-11-02T17:34:14.637Z' } },
// the search results yield a less optimal
{ package: { name: '@salesforce/foo', date: '2020-11-02T17:34:14.637Z' } },
],
},
],
[
{ name: '@salesforce/bar', description: lorem, homepage: 'https://www.salesforce.com' },
{ downloads: '100' },
{
objects: [
{ package: { name: '@salesforce/bar', date: '2021-11-02T17:34:14.637Z' } },
// the search results yield a less optimal
{ package: { name: '@salesforce/foo', date: '2020-11-02T17:34:14.637Z' } },
],
},
],
[
{ name: '@salesforce/nodate', description: lorem, homepage: 'https://www.salesforce.com' },
{ downloads: '1' },
{
objects: [
{ package: { name: '@salesforce/bar', date: '2021-11-02T17:34:14.637Z' } },
// the search results yield a less optimal
{ package: { name: '@salesforce/foo', date: '2020-11-02T17:34:14.637Z' } },
],
},
],
]);
});

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

it('plugins:discover returns a table', async () => {
await Discover.run([]);

expect(sfCommandStubs.table.callCount).to.equal(1);
});

it('plugins:discover returns proper json', async () => {
const result = await Discover.run(['--json']);
expect(result.length).to.equal(3);
result.map((pkg) => {
expect(pkg).to.have.all.keys('name', 'description', 'homepage', 'downloads', 'date');
});
});
});
112 changes: 112 additions & 0 deletions test/commands/discoverQuery.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* Copyright (c) 2023, salesforce.com, inc.
* All rights reserved.
* 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 { expect, config } from 'chai';
import { transform, descriptionTransform, query } from '../../src/shared/discoverQuery';
config.truncateThreshold = 0;

describe('query tranformations', () => {
it('sorted by downloads', () => {
const result = [
[
{ name: '@salesforce/foo', description: 'blah', homepage: 'https://www.salesforce.com' },
{ downloads: '10' },
{
objects: [
{ package: { name: '@salesforce/bar', date: '2021-11-02T17:34:14.637Z' } },
// the search results yield a less optimal
{ package: { name: '@salesforce/foo', date: '2020-11-02T17:34:14.637Z' } },
],
},
],
[
{ name: '@salesforce/bar', description: 'blah', homepage: 'https://www.salesforce.com' },
{ downloads: '100' },
{
objects: [
{ package: { name: '@salesforce/bar', date: '2021-11-02T17:34:14.637Z' } },
// the search results yield a less optimal
{ package: { name: '@salesforce/foo', date: '2020-11-02T17:34:14.637Z' } },
],
},
],
[
{ name: '@salesforce/nodate', description: 'blah', homepage: 'https://www.salesforce.com' },
{ downloads: '1' },
{
objects: [
{ package: { name: '@salesforce/bar', date: '2021-11-02T17:34:14.637Z' } },
// the search results yield a less optimal
{ package: { name: '@salesforce/foo', date: '2020-11-02T17:34:14.637Z' } },
],
},
],
] satisfies Awaited<ReturnType<typeof query>>;
const out = transform(result);
const barIndex = out.findIndex((r) => r.name === '@salesforce/bar');
const fooIndex = out.findIndex((r) => r.name === '@salesforce/foo');
const nodateIndex = out.findIndex((r) => r.name === '@salesforce/nodate');

expect(nodateIndex).to.be.greaterThan(-1);
expect(barIndex).to.be.greaterThan(-1);
expect(fooIndex).to.be.greaterThan(-1);

expect(barIndex).to.be.lessThan(fooIndex);
expect(fooIndex).to.be.lessThan(nodateIndex);
});

it('handles no matching search results', () => {
expect(
transform([
[
{ name: '@salesforce/nodate', description: 'blah', homepage: 'https://www.salesforce.com' },
{ downloads: '1' },
{
objects: [
{ package: { name: '@salesforce/bar', date: '2021-11-02T17:34:14.637Z' } },
{ package: { name: '@salesforce/foo', date: '2020-11-02T17:34:14.637Z' } },
],
},
],
])[0].date
).to.equal('');
});
it('handles no search results', () => {
expect(
transform([
[
{ name: '@salesforce/nodate', description: 'blah', homepage: 'https://www.salesforce.com' },
{ downloads: '1' },
{
objects: [],
},
],
])[0].date
).to.equal('');
});

describe('description transform', () => {
it('removes markdown dividers', () => {
expect(descriptionTransform('blah ================blah regular 2=2')).to.not.include('==');
expect(descriptionTransform('blah ================blah regular 2=2')).to.include('2=2');
});
it('removes empty lines', () => {
expect(
descriptionTransform(`blah
blah`)
).to.not.include('\n\n');
});
it('handles long descriptions search results', () => {
const lorem = `Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus a magna odio. Nullam bibendum magna a lectus faucibus, et viverra neque ornare. Vestibulum tellus augue, rutrum et semper id, pellentesque eget lacus. Nunc ac ultricies nulla, in sagittis arcu. Maecenas accumsan ac dui eget pharetra. Interdum et malesuada fames ac ante ipsum primis in faucibus. Quisque ac nisi pretium, suscipit metus ac, sodales nisl. Quisque eget finibus nunc, id iaculis quam. Fusce a ultrices leo. Morbi eu est condimentum sapien semper porttitor non ac nisl. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.
Praesent viverra interdum enim commodo ullamcorper. Suspendisse potenti. Suspendisse tristique dignissim vestibulum. Maecenas non velit faucibus, dapibus tellus sit amet, hendrerit lacus. Nullam metus nunc, suscipit venenatis tempor eu, facilisis pulvinar tellus. Proin venenatis pretium justo, at venenatis sapien pharetra porttitor. Cras egestas eget mi tristique ullamcorper. Etiam pretium non risus quis maximus. Fusce in pulvinar ipsum, in elementum magna. Donec nec lacus sit amet est fringilla vehicula sit amet ac ante. Fusce non laoreet arcu. Sed nisi dolor, tempus nec tortor non, placerat tincidunt ligula.
`;
expect(Array.from(descriptionTransform(lorem).matchAll(/\n/g))).to.have.length.greaterThanOrEqual(10);
});
});
});
51 changes: 0 additions & 51 deletions test/commands/hello/world.test.ts

This file was deleted.

0 comments on commit c7bed7c

Please sign in to comment.