Skip to content

Commit

Permalink
unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yavorsk committed Oct 15, 2024
1 parent 4bae911 commit e127fec
Show file tree
Hide file tree
Showing 6 changed files with 99,633 additions and 132 deletions.
170 changes: 39 additions & 131 deletions packages/sitecore-jss-dev-tools/src/templating/metadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
/* eslint-disable no-unused-expressions */
import { expect } from 'chai';
import fs from 'fs';
import path from 'path';
import { getMetadata } from './metadata';
import sinon, { SinonStub } from 'sinon';
import { Metadata } from '@sitecore-jss/sitecore-jss/editing';
import childProcess from 'child_process';
import packageLockNextjs from './../test-data/metadata/package-lock-nextjs.json';
import packageLockAngular from './../test-data/metadata/package-lock-angular.json';
import packageLockNoSitecore from './../test-data/metadata/package-lock-no-sitecore-scopes.json';
import metadataNextjs from './../test-data/metadata/metadata-nextjs.json';
import metadataAngular from './../test-data/metadata/metadata-angular.json';

describe('metadata', () => {
afterEach(() => {
Expand All @@ -28,160 +29,67 @@ describe('metadata', () => {
execSyncStub?.restore();
});

it('should return packages metadata from @sitecore scope', () => {
readdirSync = sinon.stub(fs, 'readdirSync');
readdirSync.withArgs('node_modules').returns(['@sitecore']);
readdirSync.withArgs(path.join('node_modules', '@sitecore')).returns(['byoc', 'components']);

readFileSync = sinon.stub(fs, 'readFileSync');
readFileSync
.withArgs(path.join('node_modules', '@sitecore', 'byoc', 'package.json'))
.returns('{"name": "@sitecore/byoc","version": "0.2.8"}');
readFileSync
.withArgs(path.join('node_modules', '@sitecore', 'components', 'package.json'))
.returns('{"name": "@sitecore/components","version": "1.1.6"}');

const expected: Metadata = {
packages: {
'@sitecore/byoc': '0.2.8',
'@sitecore/components': '1.1.6',
},
};

const packagesMetadata = getMetadata();
expect(packagesMetadata).to.deep.equal(expected);
});

it('should return packages metadata from @sitecore-jss scope', () => {
readdirSync = sinon.stub(fs, 'readdirSync');
readdirSync.withArgs('node_modules').returns(['@sitecore-jss']);
readdirSync
.withArgs(path.join('node_modules', '@sitecore-jss'))
.returns(['sitecore-jss-cli', 'sitecore-jss-nextjs']);

readFileSync = sinon.stub(fs, 'readFileSync');
readFileSync
.withArgs(path.join('node_modules', '@sitecore-jss', 'sitecore-jss-cli', 'package.json'))
.returns('{"name": "@sitecore-jss/sitecore-jss-cli","version": "21.7.0-canary.55"}');
readFileSync
.withArgs(path.join('node_modules', '@sitecore-jss', 'sitecore-jss-nextjs', 'package.json'))
.returns('{"name": "@sitecore-jss/sitecore-jss-nextjs","version": "21.7.0-canary.55"}');

const expected: Metadata = {
packages: {
'@sitecore-jss/sitecore-jss-cli': '21.7.0-canary.55',
'@sitecore-jss/sitecore-jss-nextjs': '21.7.0-canary.55',
},
};

const packagesMetadata = getMetadata();
expect(packagesMetadata).to.deep.equal(expected);
});

it('should return packages metadata from @sitecore-cloudsdk scope', () => {
readdirSync = sinon.stub(fs, 'readdirSync');
readdirSync.withArgs('node_modules').returns(['@sitecore-cloudsdk']);
readdirSync.withArgs(path.join('node_modules', '@sitecore-cloudsdk')).returns(['core']);

readFileSync = sinon.stub(fs, 'readFileSync');
readFileSync
.withArgs(path.join('node_modules', '@sitecore-cloudsdk', 'core', 'package.json'))
.returns('{"name": "@sitecore-cloudsdk/core","version": "0.1.5"}');

const expected: Metadata = {
packages: {
'@sitecore-cloudsdk/core': '0.1.5',
},
};

const packagesMetadata = getMetadata();
expect(packagesMetadata).to.deep.equal(expected);
});

it('should return packages metadata from @sitecore-feaas scope', () => {
readdirSync = sinon.stub(fs, 'readdirSync');
readdirSync.withArgs('node_modules').returns(['@sitecore-feaas']);
readdirSync.withArgs(path.join('node_modules', '@sitecore-feaas')).returns(['clientside']);

it('it should create package-lock.json if not already present and then remove it', () => {
existsSync = sinon.stub(fs, 'existsSync');
existsSync.withArgs('./package-lock.json').returns(false);
readFileSync = sinon.stub(fs, 'readFileSync');
readFileSync
.withArgs(path.join('node_modules', '@sitecore-feaas', 'clientside', 'package.json'))
.returns('{"name": "@sitecore-feaas/clientside","version": "0.5.12"}');

const expected: Metadata = {
packages: {
'@sitecore-feaas/clientside': '0.5.12',
},
};

const packagesMetadata = getMetadata();
expect(packagesMetadata).to.deep.equal(expected);
readFileSync.withArgs('./package-lock.json', 'utf8').returns(JSON.stringify({}));
execSyncStub = sinon.stub(childProcess, 'execSync');
getMetadata();
expect(execSyncStub.calledTwice).to.be.true;
});

it('should not return packages metadata for not tracked scopes', () => {
const scope = '@nottracked-scope';
readdirSync = sinon.stub(fs, 'readdirSync');
readdirSync.withArgs('node_modules').returns([scope]);

const expected: Metadata = { packages: {} };

const packagesMetadata = getMetadata();
expect(packagesMetadata).to.deep.equal(expected);
it('should thorow if package-lock creation failed', () => {
existsSync = sinon.stub(fs, 'existsSync');
existsSync.withArgs('./package-lock.json').returns(false);
execSyncStub = sinon.stub(childProcess, 'execSync').throws('error');
expect(() => getMetadata()).to.throw;
});

it('should throw if package.json not found', () => {
readdirSync = sinon.stub(fs, 'readdirSync');
readdirSync.withArgs('node_modules').returns(['@sitecore-feaas']);
readdirSync.withArgs(path.join('node_modules', '@sitecore-feaas')).returns(['clientside']);

it('should return tracked packages from example nextjs package-lock.json', () => {
existsSync = sinon.stub(fs, 'existsSync');
existsSync.withArgs('./package-lock.json').returns(true);
readFileSync = sinon.stub(fs, 'readFileSync');
readFileSync
.withArgs(path.join('node_modules', '@sitecore-feaas', 'clientside', 'package.json'))
.returns(null);
.withArgs('./package-lock.json', 'utf8')
.returns(JSON.stringify(packageLockNextjs));

expect(() => getMetadata()).to.throw;
const metadata = getMetadata();
expect(metadata).to.deep.equal(metadataNextjs);
});

it('should throw if json not valid', () => {
readdirSync = sinon.stub(fs, 'readdirSync');
readdirSync.withArgs('node_modules').returns(['@sitecore-feaas']);
readdirSync.withArgs(path.join('node_modules', '@sitecore-feaas')).returns(['clientside']);

it('should return tracked packages from example angular package-lock.json', () => {
existsSync = sinon.stub(fs, 'existsSync');
existsSync.withArgs('./package-lock.json').returns(true);
readFileSync = sinon.stub(fs, 'readFileSync');
readFileSync
.withArgs(path.join('node_modules', '@sitecore-feaas', 'clientside', 'package.json'))
.returns('{"name": "@sitecore-feaas/clientside","version": "0.5.12"');
.withArgs('./package-lock.json', 'utf8')
.returns(JSON.stringify(packageLockAngular));

expect(() => getMetadata()).to.throw;
const metadata = getMetadata();
expect(metadata).to.deep.equal(metadataAngular);
});

it('new test: it should create package-lock.json if not already present and then remove it', () => {
it('should not return packages for package-lock not containng tracked packages', () => {
existsSync = sinon.stub(fs, 'existsSync');
existsSync.withArgs('./package-lock.json').returns(false);
existsSync.withArgs('./package-lock.json').returns(true);
readFileSync = sinon.stub(fs, 'readFileSync');
readFileSync.withArgs('./package-lock.json', 'utf8').returns(JSON.stringify({}));
execSyncStub = sinon.stub(childProcess, 'execSync');
getMetadata();
expect(execSyncStub.calledTwice).to.be.true;
});
readFileSync
.withArgs('./package-lock.json', 'utf8')
.returns(JSON.stringify(packageLockNoSitecore));

it('new test: should thorow if package-lock creation failed', () => {
existsSync = sinon.stub(fs, 'existsSync');
existsSync.withArgs('./package-lock.json').returns(false);
execSyncStub = sinon.stub(childProcess, 'execSync').throws('error');
expect(() => getMetadata()).to.throw;
const metadata = getMetadata();
expect(metadata).to.deep.equal({ packages: {} });
});

it('new test: should return tracked packages from example nextjs package-lock.json', () => {
it('should throw if package-lock json not valid', () => {
existsSync = sinon.stub(fs, 'existsSync');
existsSync.withArgs('./package-lock.json').returns(true);
readFileSync = sinon.stub(fs, 'readFileSync');
readFileSync
.withArgs('./package-lock.json', 'utf8')
.returns(JSON.stringify(packageLockNextjs));

const metadata = getMetadata();
expect(metadata).to.deep.equal(metadataNextjs);
.returns(fs.readFileSync('./../test-data/metadata/package-lock-invalid.json', 'utf8'));
expect(() => getMetadata()).to.throw;
});
});
});
4 changes: 3 additions & 1 deletion packages/sitecore-jss-dev-tools/src/templating/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export function getMetadata(): Metadata {

if (!packageLockIsAvailable) {
try {
execSync('npm i --package-lock-only --workspaces false');
console.log('Creating package-lock.json ...');
execSync('npm i --package-lock-only --workspaces false --force');
} catch (error) {
console.error(`Failed to create package-lock.json in project`, error);
return metadata;
Expand All @@ -40,6 +41,7 @@ export function getMetadata(): Metadata {
metadata.packages = getPackagesFromPackageLock(packageLock);

if (!packageLockIsAvailable) {
console.log('Deleting package-lock.json ...');
execSync(`del "${packageLockPath}"`);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"packages": {
"@sitecore-jss/sitecore-jss-angular": "22.2.0-canary.69",
"@sitecore-jss/sitecore-jss-angular-schematics": "22.2.0-canary.69",
"@sitecore-jss/sitecore-jss-cli": "22.2.0-canary.69",
"@sitecore-jss/sitecore-jss-dev-tools": "22.2.0-canary.69",
"@sitecore-jss/sitecore-jss": "22.2.0-canary.69"
}
}
Loading

0 comments on commit e127fec

Please sign in to comment.