Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/epic/quick-order' into feature/G…
Browse files Browse the repository at this point in the history
…H-11818-feature-lib-for-quick-order
  • Loading branch information
dydome committed Apr 7, 2021
2 parents 5ac5c16 + da8ea4f commit 741b41d
Show file tree
Hide file tree
Showing 129 changed files with 3,406 additions and 2,336 deletions.
10 changes: 10 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@
"sourceMapPathOverrides": {
"": "${webRoot}/"
}
},
{
"type": "node",
"request": "launch",
"name": "Debug schematics",
"skipFiles": ["<node_internals>/**"],
"program": "${workspaceFolder}/node_modules/@angular/cli/bin/ng",
"args": ["add", "@spartacus/schematics@latest"],
"console": "integratedTerminal",
"outFiles": ["${workspaceFolder}/**/*.js"]
}
]
}
2 changes: 1 addition & 1 deletion ci-scripts/unit-tests-sonar.sh
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ fi

echo "Running unit tests and code coverage for schematics library"
exec 5>&1
output=$(yarn --cwd projects/schematics run test --coverage=true | tee /dev/fd/5)
output=$(yarn --cwd projects/schematics run test --runInBand --coverage=true | tee /dev/fd/5)

if [[ $1 == '-h' ]]; then
echo "Usage: $0 [sonar (to run sonar scan)]"
Expand Down
4 changes: 2 additions & 2 deletions feature-libs/asm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@
"@angular/forms": "^10.2.4",
"@ngrx/effects": "^10.0.0",
"@ngrx/store": "^10.0.0",
"@schematics/angular": "10.2.1",
"@spartacus/core": "3.2.0-next.2",
"@spartacus/schematics": "3.2.0-next.2",
"@spartacus/storefront": "3.2.0-next.2",
"rxjs": "^6.6.0"
},
"publishConfig": {
"access": "public"
}
},
"schematics": "./schematics/collection.json"
}
45 changes: 6 additions & 39 deletions feature-libs/asm/schematics/add-asm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,20 @@ import {
SchematicContext,
Tree,
} from '@angular-devkit/schematics';
import {
NodeDependency,
NodeDependencyType,
} from '@schematics/angular/utility/dependencies';
import {
addLibraryFeature,
addPackageJsonDependencies,
DEFAULT_B2B_OCC_CONFIG,
getAppModule,
getSpartacusSchematicsVersion,
installPackageJsonDependencies,
LibraryOptions as SpartacusAsmOptions,
readPackageJson,
SPARTACUS_SETUP,
SPARTACUS_ASM,
validateSpartacusInstallation,
} from '@spartacus/schematics';
import {
ASM_FEATURE_NAME,
ASM_FOLDER_NAME,
ASM_MODULE,
ASM_ROOT_MODULE,
ASM_TRANSLATIONS,
ASM_TRANSLATION_CHUNKS_CONFIG,
SPARTACUS_ASM,
SPARTACUS_ASM_ASSETS,
SPARTACUS_ASM_ROOT,
} from '../constants';
Expand All @@ -36,26 +27,14 @@ export function addAsmFeatures(options: SpartacusAsmOptions): Rule {
const packageJson = readPackageJson(tree);
validateSpartacusInstallation(packageJson);

const appModulePath = getAppModule(tree, options.project);

return chain([
addAsmFeature(appModulePath, options),
addAsmPackageJsonDependencies(packageJson),
installPackageJsonDependencies(),
]);
return chain([addAsmFeature(options)]);
};
}

function addAsmFeature(
appModulePath: string,
options: SpartacusAsmOptions
): Rule {
return addLibraryFeature(appModulePath, options, {
function addAsmFeature(options: SpartacusAsmOptions): Rule {
return addLibraryFeature(options, {
folderName: ASM_FOLDER_NAME,
name: ASM_FEATURE_NAME,
defaultConfig: {
name: DEFAULT_B2B_OCC_CONFIG,
importPath: SPARTACUS_SETUP,
},
featureModule: {
name: ASM_MODULE,
importPath: SPARTACUS_ASM,
Expand All @@ -71,15 +50,3 @@ function addAsmFeature(
},
});
}

function addAsmPackageJsonDependencies(packageJson: any): Rule {
const spartacusVersion = `^${getSpartacusSchematicsVersion()}`;
const dependencies: NodeDependency[] = [
{
type: NodeDependencyType.Default,
version: spartacusVersion,
name: SPARTACUS_ASM,
},
];
return addPackageJsonDependencies(dependencies, packageJson);
}
101 changes: 35 additions & 66 deletions feature-libs/asm/schematics/add-asm/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,38 @@ import {
UnitTestTree,
} from '@angular-devkit/schematics/testing';
import {
Schema as ApplicationOptions,
Style,
} from '@schematics/angular/application/schema';
import { Schema as WorkspaceOptions } from '@schematics/angular/workspace/schema';
import {
CLI_ASM_FEATURE,
LibraryOptions as SpartacusAsmOptions,
SpartacusOptions,
} from '@spartacus/schematics';
import * as path from 'path';
import { CLI_ASM_FEATURE, SPARTACUS_ASM } from '../constants';

const collectionPath = path.join(__dirname, '../collection.json');
const appModulePath = 'src/app/app.module.ts';
const asmFeatureModulePath =
'src/app/spartacus/features/asm/asm-feature.module.ts';

// TODO: Improve tests after lib-util test update
describe('Spartacus Asm schematics: ng-add', () => {
const schematicRunner = new SchematicTestRunner('schematics', collectionPath);

let appTree: UnitTestTree;

const workspaceOptions: any = {
const workspaceOptions: WorkspaceOptions = {
name: 'workspace',
version: '0.5.0',
};

const appOptions: any = {
const appOptions: ApplicationOptions = {
name: 'schematics-test',
inlineStyle: false,
inlineTemplate: false,
routing: false,
style: 'scss',
style: Style.Scss,
skipTests: false,
projectRoot: '',
};
Expand All @@ -40,17 +47,16 @@ describe('Spartacus Asm schematics: ng-add', () => {

const spartacusDefaultOptions: SpartacusOptions = {
project: 'schematics-test',
configuration: 'b2c',
lazy: true,
features: [],
};

beforeEach(async () => {
schematicRunner.registerCollection(
'@spartacus/schematics',
'../../projects/schematics/src/collection.json'
);
schematicRunner.registerCollection(
'@spartacus/storefinder',
'../../feature-libs/storefinder/schematics/collection.json'
);

appTree = await schematicRunner
.runExternalSchematicAsync(
Expand Down Expand Up @@ -88,11 +94,6 @@ describe('Spartacus Asm schematics: ng-add', () => {
)
.toPromise();
});

it('should install @spartacus/asm library', () => {
const packageJson = appTree.readContent('package.json');
expect(packageJson).toContain(SPARTACUS_ASM);
});
});

describe('eager loading', () => {
Expand All @@ -106,26 +107,19 @@ describe('Spartacus Asm schematics: ng-add', () => {
.toPromise();
});

it('should add asm deps', async () => {
const packageJson = appTree.readContent('/package.json');
const packageObj = JSON.parse(packageJson);
const depPackageList = Object.keys(packageObj.dependencies);
expect(depPackageList.includes('@spartacus/asm')).toBe(true);
});

it('should import appropriate modules', async () => {
const appModule = appTree.readContent(appModulePath);
expect(appModule).toContain(
`import { AsmRootModule } from '@spartacus/asm/root';`
const asmModule = appTree.readContent(asmFeatureModulePath);
expect(asmModule).toContain(
`import { AsmRootModule } from "@spartacus/asm/root";`
);
expect(appModule).toContain(
`import { AsmModule } from '@spartacus/asm';`
expect(asmModule).toContain(
`import { AsmModule } from "@spartacus/asm";`
);
});

it('should not contain lazy loading syntax', async () => {
const appModule = appTree.readContent(appModulePath);
expect(appModule).not.toContain(`import('@spartacus/asm').then(`);
const asmModule = appTree.readContent(asmFeatureModulePath);
expect(asmModule).not.toContain(`import('@spartacus/asm').then(`);
});
});

Expand All @@ -137,17 +131,17 @@ describe('Spartacus Asm schematics: ng-add', () => {
});

it('should import AsmRootModule and contain the lazy loading syntax', async () => {
const appModule = appTree.readContent(appModulePath);
expect(appModule).toContain(
`import { AsmRootModule } from '@spartacus/asm/root';`
const asmModule = appTree.readContent(asmFeatureModulePath);
expect(asmModule).toContain(
`import { AsmRootModule } from "@spartacus/asm/root";`
);
expect(appModule).toContain(`import('@spartacus/asm').then(`);
expect(asmModule).toContain(`import('@spartacus/asm').then(`);
});

it('should not contain the AsmModule import', () => {
const appModule = appTree.readContent(appModulePath);
expect(appModule).not.toContain(
`import { AsmModule } from '@spartacus/asm';`
const asmModule = appTree.readContent(asmFeatureModulePath);
expect(asmModule).not.toContain(
`import { AsmModule } from "@spartacus/asm";`
);
});
});
Expand All @@ -159,41 +153,16 @@ describe('Spartacus Asm schematics: ng-add', () => {
});

it('should import the i18n resource and chunk from assets', async () => {
const appModule = appTree.readContent(appModulePath);
expect(appModule).toContain(
`import { asmTranslations } from '@spartacus/asm/assets';`
);
expect(appModule).toContain(
`import { asmTranslationChunksConfig } from '@spartacus/asm/assets';`
const asmModule = appTree.readContent(asmFeatureModulePath);
expect(asmModule).toContain(
`import { asmTranslationChunksConfig, asmTranslations } from "@spartacus/asm/assets";`
);
});
it('should provideConfig', async () => {
const appModule = appTree.readContent(appModulePath);
expect(appModule).toContain(`resources: asmTranslations,`);
expect(appModule).toContain(`chunks: asmTranslationChunksConfig,`);
const asmModule = appTree.readContent(asmFeatureModulePath);
expect(asmModule).toContain(`resources: asmTranslations,`);
expect(asmModule).toContain(`chunks: asmTranslationChunksConfig,`);
});
});
});

describe('when other Spartacus features are already installed', () => {
beforeEach(async () => {
appTree = await schematicRunner
.runExternalSchematicAsync(
'@spartacus/storefinder',
'ng-add',
{ ...spartacusDefaultOptions, name: 'schematics-test' },
appTree
)
.toPromise();
appTree = await schematicRunner
.runSchematicAsync('ng-add', defaultOptions, appTree)
.toPromise();
});

it('should just append asm feature without duplicating the featureModules config', () => {
const appModule = appTree.readContent(appModulePath);
expect(appModule.match(/featureModules:/g)?.length).toEqual(1);
expect(appModule).toContain(`asm: {`);
});
});
});
5 changes: 5 additions & 0 deletions feature-libs/asm/schematics/collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
"private": true,
"hidden": true,
"aliases": ["install"]
},
"add": {
"factory": "./add-asm/index#addAsmFeatures",
"description": "Add and configure Spartacus' Asm features",
"schema": "./add-asm/schema.json"
}
}
}
6 changes: 3 additions & 3 deletions feature-libs/asm/schematics/constants.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { SPARTACUS_ASM } from '@spartacus/schematics';

export const ASM_FOLDER_NAME = 'asm';
export const ASM_MODULE = 'AsmModule';
export const ASM_FEATURE_NAME = 'asm';
export const ASM_ROOT_MODULE = 'AsmRootModule';
export const SPARTACUS_ASM = '@spartacus/asm';
export const SPARTACUS_ASM_ROOT = `${SPARTACUS_ASM}/root`;
export const SPARTACUS_ASM_ASSETS = `${SPARTACUS_ASM}/assets`;
export const ASM_TRANSLATIONS = 'asmTranslations';
export const ASM_TRANSLATION_CHUNKS_CONFIG = 'asmTranslationChunksConfig';

export const CLI_ASM_FEATURE = 'asm';
2 changes: 1 addition & 1 deletion feature-libs/asm/tsconfig.schematics.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"resolveJsonModule": true,
"esModuleInterop": true,
"paths": {
"@spartacus/schematics": ["../../projects/schematics/src/public_api"]
"@spartacus/schematics": ["../../projects/schematics/index"]
}
},
"include": ["schematics/**/*.ts"],
Expand Down
4 changes: 1 addition & 3 deletions feature-libs/cart/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,12 @@
"@angular/router": "^10.1.0",
"@ngrx/effects": "^10.0.0",
"@ngrx/store": "^10.0.0",
"@schematics/angular": "^10.1.0",
"@spartacus/core": "3.2.0-next.2",
"@spartacus/schematics": "3.2.0-next.2",
"@spartacus/storefront": "3.2.0-next.2",
"@spartacus/styles": "3.2.0-next.2",
"bootstrap": "^4.0",
"rxjs": "^6.6.0",
"typescript": "~4.0.2"
"rxjs": "^6.6.0"
},
"publishConfig": {
"access": "public"
Expand Down
Loading

0 comments on commit 741b41d

Please sign in to comment.