diff --git a/src/components/__tests__/__snapshots__/DiscriminatorDropdown.test.tsx.snap b/src/components/__tests__/__snapshots__/DiscriminatorDropdown.test.tsx.snap index 5f865af5..9274ee26 100644 --- a/src/components/__tests__/__snapshots__/DiscriminatorDropdown.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/DiscriminatorDropdown.test.tsx.snap @@ -292,6 +292,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView }, "unstable_ignoreMimeParameters": false, "untrustedSpec": false, + "userQueryParamToNavigate": false, }, "pattern": undefined, "pointer": "#/components/schemas/Dog/properties/packSize", @@ -563,6 +564,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView }, "unstable_ignoreMimeParameters": false, "untrustedSpec": false, + "userQueryParamToNavigate": false, }, "pattern": undefined, "pointer": "#/components/schemas/Dog/properties/type", @@ -821,6 +823,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView }, "unstable_ignoreMimeParameters": false, "untrustedSpec": false, + "userQueryParamToNavigate": false, }, "pattern": undefined, "pointer": "#/components/schemas/Dog", @@ -1141,6 +1144,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView }, "unstable_ignoreMimeParameters": false, "untrustedSpec": false, + "userQueryParamToNavigate": false, }, "pattern": undefined, "pointer": "#/components/schemas/Cat/properties/type", @@ -1424,6 +1428,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView }, "unstable_ignoreMimeParameters": false, "untrustedSpec": false, + "userQueryParamToNavigate": false, }, "pattern": undefined, "pointer": "#/components/schemas/Cat/properties/packSize", @@ -1678,6 +1683,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView }, "unstable_ignoreMimeParameters": false, "untrustedSpec": false, + "userQueryParamToNavigate": false, }, "pattern": undefined, "pointer": "#/components/schemas/Cat", @@ -1957,6 +1963,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView }, "unstable_ignoreMimeParameters": false, "untrustedSpec": false, + "userQueryParamToNavigate": false, }, "pattern": undefined, "pointer": "#/components/schemas/Pet", @@ -2266,6 +2273,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView }, "unstable_ignoreMimeParameters": false, "untrustedSpec": false, + "userQueryParamToNavigate": false, }, "pattern": undefined, "pointer": "#/components/schemas/Dog/properties/packSize", @@ -2537,6 +2545,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView }, "unstable_ignoreMimeParameters": false, "untrustedSpec": false, + "userQueryParamToNavigate": false, }, "pattern": undefined, "pointer": "#/components/schemas/Dog/properties/type", @@ -2795,6 +2804,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView }, "unstable_ignoreMimeParameters": false, "untrustedSpec": false, + "userQueryParamToNavigate": false, }, "pattern": undefined, "pointer": "#/components/schemas/Dog", diff --git a/src/services/HistoryService.ts b/src/services/HistoryService.ts index 8565c203..cd89dc07 100644 --- a/src/services/HistoryService.ts +++ b/src/services/HistoryService.ts @@ -94,6 +94,6 @@ export class HistoryService { } private getHrefSplitCharacter(): string { - return this.shouldQueryParamNavigationBeUsed() ? '?redoc' : '#'; + return this.shouldQueryParamNavigationBeUsed() ? '?redoc=' : '#'; } } diff --git a/src/services/__tests__/history.service.test.ts b/src/services/__tests__/history.service.test.ts index e52664da..1c5c46e4 100644 --- a/src/services/__tests__/history.service.test.ts +++ b/src/services/__tests__/history.service.test.ts @@ -1,13 +1,32 @@ -import { history } from '../HistoryService'; +import { HistoryService } from '../HistoryService'; +import { RedocNormalizedOptions } from '../RedocNormalizedOptions'; + +const options = new RedocNormalizedOptions({}); describe('History service', () => { + function mockWindowLocationForSearch(): void { + const mockResponse = jest.fn(); + Object.defineProperty(window, 'location', { + value: { + hash: { + endsWith: mockResponse, + includes: mockResponse, + }, + assign: mockResponse, + }, + writable: true, + }); + } + test('should be an instance', () => { + const history = new HistoryService(options); expect(typeof history).not.toBe('function'); expect(history.subscribe).toBeDefined(); }); test('History subscribe', () => { const fn = jest.fn(); + const history = new HistoryService(options); history.subscribe(fn); history.emit(); expect(fn).toHaveBeenCalled(); @@ -15,6 +34,7 @@ describe('History service', () => { test('History subscribe should return unsubscribe function', () => { const fn = jest.fn(); + const history = new HistoryService(options); const unsubscribe = history.subscribe(fn); history.emit(); expect(fn).toHaveBeenCalled(); @@ -23,16 +43,44 @@ describe('History service', () => { expect(fn).toHaveBeenCalledTimes(1); }); - test('currentId should return correct id', () => { - window.location.hash = '#testid'; - expect(history.currentId).toEqual('testid'); + describe('History with config property: `userQueryParamToNavigate` false', () => { + test('currentId should return correct id', () => { + window.location.hash = '#testid'; + const history = new HistoryService(options); + expect(history.currentId).toEqual('testid'); + }); + + test('should return correct link for id', () => { + const history = new HistoryService(options); + expect(history.linkForId('testid')).toEqual('#testid'); + }); + + test('should return empty link for empty id', () => { + const history = new HistoryService(options); + expect(history.linkForId('')).toEqual(''); + }); }); - test('should return correct link for id', () => { - expect(history.linkForId('testid')).toEqual('#testid'); - }); + describe('History with config property: `userQueryParamToNavigate` true', () => { + const overrideOptions = new RedocNormalizedOptions({ + userQueryParamToNavigate: true, + }); - test('should return empty link for empty id', () => { - expect(history.linkForId('')).toEqual(''); + test('currentId should return correct id', () => { + mockWindowLocationForSearch(); + window.location.search = '?redoc=testid'; + const history = new HistoryService(overrideOptions); + expect(history.currentId).toEqual('testid'); + }); + + test('should return correct link for id', () => { + const history = new HistoryService(overrideOptions); + expect(history.linkForId('testid')).toEqual('?redoc=testid'); + }); + + test('should return empty link for empty id', () => { + const history = new HistoryService(overrideOptions); + expect(history.linkForId('')).toEqual(''); + }); }); });