Skip to content

Commit

Permalink
feat(history-service): support setting a hash by the primary consumer (
Browse files Browse the repository at this point in the history
  • Loading branch information
unstubbable authored May 3, 2019
1 parent 842e485 commit 8a73593
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,21 +88,22 @@ describe('#createRootLocationTransformer', () => {
});

describe('with only a primary', () => {
it('puts the location pathname and query params directly to the root location', () => {
it('puts the location pathname, query params, and hash directly to the root location', () => {
const locationTransformer = createRootLocationTransformer({
consumerPathsQueryParamName: '---',
primaryConsumerUid: 'test:pri'
});

const rootLocation = locationTransformer.createRootLocation(
{pathname: '/foo', search: 'bar=1&baz=2'} as Location,
{pathname: '/foo', search: 'bar=1&baz=2', hash: '#qux'} as Location,
{pathname: '/'} as Location,
'test:pri'
);

expect(rootLocation).toMatchObject({
pathname: '/foo',
search: 'bar=1&baz=2'
search: 'bar=1&baz=2',
hash: '#qux'
});
});

Expand All @@ -113,7 +114,7 @@ describe('#createRootLocationTransformer', () => {
});

let rootLocation = locationTransformer.createRootLocation(
{pathname: '/foo'} as Location,
{pathname: '/foo', search: 'bar=1&baz=2', hash: '#qux'} as Location,
{pathname: '/'} as Location,
'test:pri'
);
Expand All @@ -124,10 +125,7 @@ describe('#createRootLocationTransformer', () => {
'test:pri'
);

expect(rootLocation).toMatchObject({
pathname: '/',
search: ''
});
expect(rootLocation).toMatchObject({pathname: '/', search: ''});
});

describe('when the primary tries to set a query param that conflicts with the consumer paths query param', () => {
Expand All @@ -153,49 +151,35 @@ describe('#createRootLocationTransformer', () => {
});

describe('with the primary and two other consumers', () => {
it('takes the pathname and query params of the primary consumer directly, and the pathname and query params of the other consumers encoded as a single query param, into the root location', () => {
it('takes the pathname, query params, and hash of the primary consumer directly, and the pathname and query params of the other consumers encoded as a single query param, into the root location', () => {
const locationTransformer = createRootLocationTransformer({
consumerPathsQueryParamName: '---',
primaryConsumerUid: 'test:pri'
});

let rootLocation = locationTransformer.createRootLocation(
{pathname: '/foo', search: 'bar=1'} as Location,
{pathname: '/baz', search: 'qux=3'} as Location,
{pathname: '/'} as Location,
'test:pri'
);

rootLocation = locationTransformer.createRootLocation(
{
pathname: '/baz',
search: 'qux=3'
} as Location,
rootLocation as Location,
'test:1'
);

rootLocation = locationTransformer.createRootLocation(
{
pathname: '/some',
search: 'thing=else'
} as Location,
{pathname: '/foo', search: 'bar=1', hash: '#qux'} as Location,
rootLocation as Location,
'test:2'
'test:pri'
);

rootLocation = locationTransformer.createRootLocation(
{
pathname: '/foo',
search: 'bar=2'
} as Location,
{pathname: '/some', search: 'thing=else'} as Location,
rootLocation as Location,
'test:pri'
'test:2'
);

expect(rootLocation).toMatchObject({
pathname: '/foo',
search:
'bar=2&---=%7B%22test%3A1%22%3A%22%2Fbaz%3Fqux%3D3%22%2C%22test%3A2%22%3A%22%2Fsome%3Fthing%3Delse%22%7D'
'bar=1&---=%7B%22test%3A1%22%3A%22%2Fbaz%3Fqux%3D3%22%2C%22test%3A2%22%3A%22%2Fsome%3Fthing%3Delse%22%7D',
hash: '#qux'
});
});
});
Expand Down
27 changes: 16 additions & 11 deletions packages/history-service/src/create-root-location-transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ export interface RootLocationTransformer {

createRootLocation(
consumerLocation: history.Location | undefined,
rootLocation: history.Location,
currentRootLocation: history.Location,
consumerUid: string
): history.LocationDescriptorObject;
}

function createRootLocationForPrimaryConsumer(
rootLocation: history.Location,
currentRootLocation: history.Location,
primaryConsumerLocation: history.Location | undefined,
consumerPathsQueryParamName: string
): history.LocationDescriptorObject {
const allSearchParams = createSearchParams(rootLocation);
const allSearchParams = createSearchParams(currentRootLocation);
const newSearchParams = createSearchParams(primaryConsumerLocation);

if (newSearchParams.has(consumerPathsQueryParamName)) {
Expand All @@ -55,16 +55,20 @@ function createRootLocationForPrimaryConsumer(
? primaryConsumerLocation.pathname
: '/';

return {pathname, search};
const hash = primaryConsumerLocation
? primaryConsumerLocation.hash
: undefined;

return {pathname, search, hash};
}

function createRootLocationForOtherConsumer(
rootLocation: history.Location,
currentRootLocation: history.Location,
consumerLocation: history.Location | undefined,
consumerUid: string,
consumerPathsQueryParamName: string
): history.LocationDescriptorObject {
const allSearchParams = createSearchParams(rootLocation);
const allSearchParams = createSearchParams(currentRootLocation);
const consumerPaths = allSearchParams.get(consumerPathsQueryParamName);

const newConsumerPaths = consumerLocation
Expand All @@ -82,8 +86,9 @@ function createRootLocationForOtherConsumer(
}

return {
pathname: rootLocation.pathname,
search: allSearchParams.toString()
pathname: currentRootLocation.pathname,
search: allSearchParams.toString(),
hash: currentRootLocation.hash
};
}

Expand Down Expand Up @@ -125,22 +130,22 @@ export function createRootLocationTransformer(

createRootLocation: (
consumerLocation: history.Location | undefined,
rootLocation: history.Location,
currentRootLocation: history.Location,
consumerUid: string
): history.LocationDescriptorObject => {
const {consumerPathsQueryParamName, primaryConsumerUid} = options;
const isPrimaryConsumer = consumerUid === primaryConsumerUid;

if (isPrimaryConsumer) {
return createRootLocationForPrimaryConsumer(
rootLocation,
currentRootLocation,
consumerLocation,
consumerPathsQueryParamName
);
}

return createRootLocationForOtherConsumer(
rootLocation,
currentRootLocation,
consumerLocation,
consumerUid,
consumerPathsQueryParamName
Expand Down

0 comments on commit 8a73593

Please sign in to comment.