Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

Added required parameters. #370

Merged
merged 14 commits into from
Mar 16, 2018
Merged
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
2 changes: 1 addition & 1 deletion runtime/config-params.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export type SkyuxConfigParams = string[] | {[key: string]: boolean | {value?: any}};
export type SkyuxConfigParams = string[] | { [key: string]: boolean | { value?: any; required?: boolean } };
55 changes: 55 additions & 0 deletions runtime/params.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,59 @@ describe('SkyAppRuntimeConfigParams', () => {
expect(params.get('a2')).toBe('c');
});

it('should allow queryparam values to be required', () => {
const params: SkyAppRuntimeConfigParams = new SkyAppRuntimeConfigParams(
'',
{
a1: {
value: 'test',
required: true
}
}
);

expect(params.hasAllRequiredParams()).toBe(true);
});

it('should expose a `hasAllRequiredParams` method for testing if all required params are defined', () => {
const params: SkyAppRuntimeConfigParams = new SkyAppRuntimeConfigParams(
'',
{
a1: {
required: true
}
}
);

expect(params.hasAllRequiredParams()).toBe(false);
});

it('should expose a `hasAllRequiredParams` method that returns true if no required params are defined', () => {
const params: SkyAppRuntimeConfigParams = new SkyAppRuntimeConfigParams(
'',
{
a1: true
}
);

expect(params.hasAllRequiredParams()).toBe(true);
});

it('should expose a `hasAllRequiredParams` method that returns false if any required params are undefined', () => {
const params: SkyAppRuntimeConfigParams = new SkyAppRuntimeConfigParams(
'',
{
a1: {
value: '1',
required: true
},
a2: {
required: true
}
}
);

expect(params.hasAllRequiredParams()).toBe(false);
});

});
24 changes: 21 additions & 3 deletions runtime/params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ function getUrlSearchParams(url: string): URLSearchParams {
}

export class SkyAppRuntimeConfigParams {

private params: {[key: string]: string} = {};
private params: { [key: string]: string } = {};
private requiredParams: string[] = [];

constructor(
url: string,
Expand Down Expand Up @@ -49,6 +49,10 @@ export class SkyAppRuntimeConfigParams {
if (typeof configParam === 'object') {
const paramValue = configParam.value;

if (configParam.required) {
this.requiredParams.push(paramName);
}

if (paramValue) {
this.params[paramName] = paramValue;
}
Expand Down Expand Up @@ -83,6 +87,21 @@ export class SkyAppRuntimeConfigParams {
return this.params && this.params.hasOwnProperty(key);
}

/**
* Are all the required params defined?.
* @name hasAllRequiredParams
* @returns {array}
*/
public hasAllRequiredParams(): boolean {
if (this.requiredParams.length === 0) {
return true;
}

return this.requiredParams.every((param: string) => {
return this.params[param] !== undefined;
});
}

/**
* Returns the value of the requested param.
* @name get
Expand Down Expand Up @@ -132,5 +151,4 @@ export class SkyAppRuntimeConfigParams {

return joined.length === 0 ? url : `${url}${delimiter}${joined.join('&')}`;
}

}
52 changes: 34 additions & 18 deletions src/app/app.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import { AppComponent } from './app.component';

describe('AppComponent', () => {
let mockSkyuxHost: any;
let mockWindow: any;
let comp: AppComponent;
let fixture: ComponentFixture<AppComponent>;
let parseParams: any;
Expand All @@ -47,12 +48,20 @@ describe('AppComponent', () => {
let skyAppConfig: any;
let viewport: SkyAppViewportService;

const location = 'my-custom-location';

class MockHelpInitService {
public load() { }
}

class MockWindow {
public nativeWindow = {
location: {
href: ''
},
SKYUX_HOST: mockSkyuxHost,
scroll: () => scrollCalled = true
};
}

const mockHelpInitService = new MockHelpInitService();

function setup(
Expand All @@ -61,6 +70,7 @@ describe('AppComponent', () => {
styleLoadPromise?: Promise<any>,
omnibarProvider?: any
) {
mockWindow = new MockWindow();
let providers: any[] = [
{
provide: Router,
Expand All @@ -78,13 +88,7 @@ describe('AppComponent', () => {
},
{
provide: SkyAppWindowRef,
useValue: {
nativeWindow: {
location: location,
SKYUX_HOST: mockSkyuxHost,
scroll: () => scrollCalled = true
}
}
useValue: mockWindow
},
{
provide: SkyAppConfig,
Expand Down Expand Up @@ -131,11 +135,11 @@ describe('AppComponent', () => {
],
providers: providers
})
.compileComponents()
.then(() => {
fixture = TestBed.createComponent(AppComponent);
comp = fixture.componentInstance;
});
.compileComponents()
.then(() => {
fixture = TestBed.createComponent(AppComponent);
comp = fixture.componentInstance;
});
}

function validateOmnibarProvider(
Expand Down Expand Up @@ -184,6 +188,7 @@ describe('AppComponent', () => {
},
params: {
has: (key: any) => false,
hasAllRequiredParams: () => true,
parse: (p: any) => parseParams = p
}
},
Expand Down Expand Up @@ -438,8 +443,8 @@ describe('AppComponent', () => {
experimental: true,
nav: {
services: [
{ },
{ }
{},
{}
]
}
};
Expand All @@ -456,7 +461,7 @@ describe('AppComponent', () => {
experimental: true,
nav: {
services: [
{ },
{},
{ selected: true }
]
}
Expand Down Expand Up @@ -701,7 +706,7 @@ describe('AppComponent', () => {

skyAppConfig.runtime.params.has = (key: any) => key === false;

mockSkyuxHost = { };
mockSkyuxHost = {};
const expectedCall = { productId: 'test-config', extends: 'bb-help', locale: '' };
skyAppConfig.skyux.help = { productId: 'test-config', extends: 'bb-help' };

Expand Down Expand Up @@ -776,6 +781,17 @@ describe('AppComponent', () => {
});
}));

it('should redirect if all required params are not defined', async(() => {
skyAppConfig.runtime.params.hasAllRequiredParams = () => false;

setup(skyAppConfig).then(() => {
fixture.detectChanges();
fixture.whenStable().then(() => {
expect(mockWindow.nativeWindow.location.href).toBe('https://host.nxt.blackbaud.com/errors/notfound');
});
});
}));

it(
'should load the omnibar when the omnibar provider\'s ready() promise is resolved',
fakeAsync(() => {
Expand Down
6 changes: 5 additions & 1 deletion src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,11 @@ export class AppComponent implements OnInit {
});
};

if (!this.config.runtime.params.hasAllRequiredParams()) {
this.windowRef.nativeWindow.location.href = 'https://host.nxt.blackbaud.com/errors/notfound';
return;
}

if (omnibarConfig) {
if (this.omnibarProvider) {
this.omnibarProvider.ready().then(loadOmnibar);
Expand All @@ -253,7 +258,6 @@ export class AppComponent implements OnInit {
}

if (helpConfig && this.helpInitService) {

if (this.config.runtime.params.has('svcid')) {
helpConfig.extends = this.config.runtime.params.get('svcid');
}
Expand Down