Skip to content

Commit

Permalink
test(common): add tests to location_spec.ts
Browse files Browse the repository at this point in the history
We add some tests to location_spec.ts to validate the behavior of the
goTo and forword methods
  • Loading branch information
aahmedayed committed Sep 18, 2020
1 parent 93aa8b8 commit 8e474e7
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 19 deletions.
1 change: 0 additions & 1 deletion packages/common/src/location/location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@ export class Location {
this._platformStrategy.back();
}

// TODO: rename this method to go
/**
* Navigate to a specific page from session history, identified by its relative position to the
* current page.
Expand Down
29 changes: 29 additions & 0 deletions packages/common/test/location/location_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,35 @@ describe('Location Class', () => {

expect(location.getState()).toEqual({url: 'test1'});
});

it('should work after using forward button', () => {
expect(location.getState()).toBe(null);

location.go('/test1', '', {url: 'test1'});
location.go('/test2', '', {url: 'test2'});
expect(location.getState()).toEqual({url: 'test2'});

location.back();
expect(location.getState()).toEqual({url: 'test1'});

location.forward();
expect(location.getState()).toEqual({url: 'test2'});
});

it('should work after using location.goTo()', () => {
expect(location.getState()).toBe(null);

location.go('/test1', '', {url: 'test1'});
location.go('/test2', '', {url: 'test2'});
location.go('/test3', '', {url: 'test3'});
expect(location.getState()).toEqual({url: 'test3'});

location.goTo(-2);
expect(location.getState()).toEqual({url: 'test1'});

location.goTo(2);
expect(location.getState()).toEqual({url: 'test3'});
});
});

describe('location.onUrlChange()', () => {
Expand Down
58 changes: 40 additions & 18 deletions packages/common/testing/src/mock_platform_location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ export const MOCK_PLATFORM_LOCATION_CONFIG =
export class MockPlatformLocation implements PlatformLocation {
private baseHref: string = '';
private hashUpdate = new Subject<LocationChangeEvent>();
private _urlChangeIndex: number = 0;
private urlChanges: {
hostname: string,
protocol: string,
Expand All @@ -127,25 +128,25 @@ export class MockPlatformLocation implements PlatformLocation {
}

get hostname() {
return this.urlChanges[0].hostname;
return this.urlChanges[this._urlChangeIndex].hostname;
}
get protocol() {
return this.urlChanges[0].protocol;
return this.urlChanges[this._urlChangeIndex].protocol;
}
get port() {
return this.urlChanges[0].port;
return this.urlChanges[this._urlChangeIndex].port;
}
get pathname() {
return this.urlChanges[0].pathname;
return this.urlChanges[this._urlChangeIndex].pathname;
}
get search() {
return this.urlChanges[0].search;
return this.urlChanges[this._urlChangeIndex].search;
}
get hash() {
return this.urlChanges[0].hash;
return this.urlChanges[this._urlChangeIndex].hash;
}
get state() {
return this.urlChanges[0].state;
return this.urlChanges[this._urlChangeIndex].state;
}


Expand Down Expand Up @@ -181,38 +182,59 @@ export class MockPlatformLocation implements PlatformLocation {
replaceState(state: any, title: string, newUrl: string): void {
const {pathname, search, state: parsedState, hash} = this.parseChanges(state, newUrl);

this.urlChanges[0] = {...this.urlChanges[0], pathname, search, hash, state: parsedState};
this.urlChanges[this._urlChangeIndex] =
{...this.urlChanges[this._urlChangeIndex], pathname, search, hash, state: parsedState};
}

pushState(state: any, title: string, newUrl: string): void {
const {pathname, search, state: parsedState, hash} = this.parseChanges(state, newUrl);
this.urlChanges.unshift({...this.urlChanges[0], pathname, search, hash, state: parsedState});
if (this._urlChangeIndex > 0) {
this.urlChanges.splice(this._urlChangeIndex + 1);
}
this.urlChanges.push(
{...this.urlChanges[this._urlChangeIndex], pathname, search, hash, state: parsedState});
this._urlChangeIndex = this.urlChanges.length - 1;
}

forward(): void {
throw new Error('Not implemented');
const oldUrl = this.url;
const oldHash = this.hash;
if (this._urlChangeIndex < this.urlChanges.length) {
this._urlChangeIndex++;
}
this.scheduleHashUpdate(oldHash, oldUrl);
}

back(): void {
const oldUrl = this.url;
const oldHash = this.hash;
this.urlChanges.shift();
const newHash = this.hash;

if (oldHash !== newHash) {
scheduleMicroTask(
() => this.hashUpdate.next(
{type: 'hashchange', state: null, oldUrl, newUrl: this.url} as LocationChangeEvent));
if (this._urlChangeIndex > 0) {
this._urlChangeIndex--;
}
this.scheduleHashUpdate(oldHash, oldUrl);
}

go(relativePosition: number = 0): void {
throw new Error('Not implemented');
const oldUrl = this.url;
const oldHash = this.hash;
const nextPageIndex = this._urlChangeIndex + relativePosition;
if (nextPageIndex >= 0 && nextPageIndex < this.urlChanges.length) {
this._urlChangeIndex = nextPageIndex;
}
this.scheduleHashUpdate(oldHash, oldUrl);
}

getState(): unknown {
return this.state;
}

private scheduleHashUpdate(oldHash: string, oldUrl: string) {
if (oldHash !== this.hash) {
scheduleMicroTask(
() => this.hashUpdate.next(
{type: 'hashchange', state: null, oldUrl, newUrl: this.url} as LocationChangeEvent));
}
}
}

export function scheduleMicroTask(cb: () => any) {
Expand Down

0 comments on commit 8e474e7

Please sign in to comment.