From e576df4695fd681f1bcbaa06753d726ab85e0f9e Mon Sep 17 00:00:00 2001 From: Brandon Stirnaman Date: Wed, 7 Jun 2017 11:41:41 -0400 Subject: [PATCH] Add e2e test for guard --- e2e/shared/common.js | 43 ++++++++++++++++++++-- e2e/shared/tests.js | 7 ++++ e2e/skyux-build-aot.e2e-spec.js | 59 +++++++++++++++++++++--------- e2e/skyux-build-jit.e2e-spec.js | 61 +++++++++++++++++++++++--------- lib/sky-pages-route-generator.js | 2 +- 5 files changed, 137 insertions(+), 35 deletions(-) diff --git a/e2e/shared/common.js b/e2e/shared/common.js index 04b8c73e..98066f93 100644 --- a/e2e/shared/common.js +++ b/e2e/shared/common.js @@ -114,7 +114,7 @@ function prepareBuild(config) { resetConfig(); // Create our server - httpServer = HttpServer.createServer({ root: tmp }); + httpServer = HttpServer.createServer({ root: tmp, cache: 0 }); return new Promise((resolve, reject) => { portfinder.getPortPromise() @@ -193,6 +193,43 @@ function writeConfigServe(port) { }); } +/** + * Write a file into the src/app folder -- Used for injecting files prior to build + * that we don't want to include in the skyux-template but need to test + */ +function writeAppFile(filePath, content) { + return new Promise((resolve, reject) => { + const resolvedFilePath = path.join(path.resolve(tmp), 'src', 'app', filePath); + fs.writeFile(resolvedFilePath, content, (err) => { + if (err) { + reject(err); + return; + } + + resolve(); + }); + }); +} + +/** + * Remove file from the src/app folder -- Used for cleaning up after we've injected + * files for a specific test or group of tests + */ +function removeAppFile(filePath) { + return new Promise((resolve, reject) => { + const resolvedFilePath = path.join(path.resolve(tmp), 'src', 'app', filePath); + + fs.unlink(resolvedFilePath, (err) => { + if (err) { + reject(err); + return; + } + + resolve(); + }); + }); +} + module.exports = { afterAll: afterAll, catchReject: catchReject, @@ -203,5 +240,7 @@ module.exports = { getExitCode: getExitCode, prepareBuild: prepareBuild, prepareServe: prepareServe, - tmp: tmp + tmp: tmp, + writeAppFile: writeAppFile, + removeAppFile: removeAppFile }; diff --git a/e2e/shared/tests.js b/e2e/shared/tests.js index a1508a4f..2cb947f4 100644 --- a/e2e/shared/tests.js +++ b/e2e/shared/tests.js @@ -46,5 +46,12 @@ module.exports = { nav.get(1).click(); expect(element(by.tagName('h1')).getText()).toBe('About our Team'); done(); + }, + + respectGuardCanActivate: (done) => { + const nav = $$('.sky-navbar-item a'); + nav.get(1).click(); + expect(element(by.tagName('h1')).getText()).toBe('SKY UX Template'); + done(); } }; diff --git a/e2e/skyux-build-aot.e2e-spec.js b/e2e/skyux-build-aot.e2e-spec.js index 3960c951..9fc82860 100644 --- a/e2e/skyux-build-aot.e2e-spec.js +++ b/e2e/skyux-build-aot.e2e-spec.js @@ -3,28 +3,55 @@ const common = require('./shared/common'); const tests = require('./shared/tests'); -describe('skyux build aot', () => { +function prepareBuild() { + const opts = { mode: 'easy', name: 'dist', compileMode: 'aot' }; + return common.prepareBuild(opts) + .catch(console.error); +} - beforeAll((done) => { - const opts = { mode: 'easy', name: 'dist', compileMode: 'aot' }; - common.prepareBuild(opts) - .then(done) - .catch(err => { - console.log(err); - done(); - }); - }); +describe('skyux build aot', () => { + describe('w/base template', () => { + beforeAll((done) => prepareBuild().then(done)); - afterAll(common.afterAll); + it('should have exitCode 0', tests.verifyExitCode); - it('should have exitCode 0', tests.verifyExitCode); + it('should generate expected static files', tests.verifyFiles); - it('should generate expected static files', tests.verifyFiles); + it('should render the home components', tests.renderHomeComponent); - it('should render the home components', tests.renderHomeComponent); + it('should render shared nav component', tests.renderSharedNavComponent); - it('should render shared nav component', tests.renderSharedNavComponent); + it('should follow routerLink and render about component', tests.followRouterLinkRenderAbout); - it('should follow routerLink and render about component', tests.followRouterLinkRenderAbout); + afterAll(common.afterAll); + }); + describe('w/guard', () => { + beforeAll((done) => { + const guard = ` +import { Injectable } from '@angular/core'; + +@Injectable() +export class AboutGuard { + canActivate(next: any, state: any) { + return false; + } +} +`; + + common.writeAppFile('about/index.guard.ts', guard) + .then(() => prepareBuild()) + .then(done) + .catch(console.error); + }); + + it('should not follow routerLink when guard returns false', tests.respectGuardCanActivate); + + afterAll((done) => { + common.removeAppFile('about/index.guard.ts') + .then(() => common.afterAll()) + .then(done) + .catch(console.error); + }); + }); }); diff --git a/e2e/skyux-build-jit.e2e-spec.js b/e2e/skyux-build-jit.e2e-spec.js index a2cfc5ed..667f4b10 100644 --- a/e2e/skyux-build-jit.e2e-spec.js +++ b/e2e/skyux-build-jit.e2e-spec.js @@ -1,30 +1,59 @@ /*jshint jasmine: true, node: true */ 'use strict'; +const fs = require('fs'); +const path = require('path'); const common = require('./shared/common'); const tests = require('./shared/tests'); -describe('skyux build jit', () => { +function prepareBuild() { + const opts = { mode: 'easy', name: 'dist', compileMode: 'jit' }; + return common.prepareBuild(opts) + .catch(err => console.error); +} - beforeAll((done) => { - const opts = { mode: 'easy', name: 'dist', compileMode: 'jit' }; - common.prepareBuild(opts) - .then(done) - .catch(err => { - console.log(err); - done(); - }); - }); +describe('skyux build jit', () => { + describe('w/base template', () => { + beforeAll((done) => prepareBuild().then(done)); - afterAll(common.afterAll); + it('should have exitCode 0', tests.verifyExitCode); - it('should have exitCode 0', tests.verifyExitCode); + it('should generate expected static files', tests.verifyFiles); - it('should generate expected static files', tests.verifyFiles); + it('should render the home components', tests.renderHomeComponent); - it('should render the home components', tests.renderHomeComponent); + it('should render shared nav component', tests.renderSharedNavComponent); - it('should render shared nav component', tests.renderSharedNavComponent); + it('should follow routerLink and render about component', tests.followRouterLinkRenderAbout); - it('should follow routerLink and render about component', tests.followRouterLinkRenderAbout); + afterAll(common.afterAll); + }); + describe('w/guard', () => { + beforeAll((done) => { + const guard = ` +import { Injectable } from '@angular/core'; + +@Injectable() +export class AboutGuard { + canActivate(next: any, state: any) { + return false; + } +} +`; + + common.writeAppFile('about/index.guard.ts', guard) + .then(() => prepareBuild()) + .then(done) + .catch(console.error); + }); + + it('should not follow routerLink when guard returns false', tests.respectGuardCanActivate); + + afterAll((done) => { + common.removeAppFile('about/index.guard.ts') + .then(() => common.afterAll()) + .then(done) + .catch(console.error); + }); + }); }); diff --git a/lib/sky-pages-route-generator.js b/lib/sky-pages-route-generator.js index c5d1231e..9f16fde4 100644 --- a/lib/sky-pages-route-generator.js +++ b/lib/sky-pages-route-generator.js @@ -189,7 +189,7 @@ function extractGuard(file) { throw new Error(`As a best practice, only export one guard per file in ${file}`); } - result = { path: file, name: match[1] }; + result = { path: path.resolve(file), name: match[1] }; } return result;