Skip to content

Commit

Permalink
[Execution Context] Update on URL change
Browse files Browse the repository at this point in the history
  • Loading branch information
afharo committed Nov 19, 2024
1 parent 3e9d77a commit 1810ac5
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

import { BehaviorSubject, firstValueFrom } from 'rxjs';
import { analyticsServiceMock } from '@kbn/core-analytics-browser-mocks';
import { applicationServiceMock } from '@kbn/core-application-browser-mocks';
import type { InternalApplicationStart } from '@kbn/core-application-browser-internal';
import type { AnalyticsServiceSetup } from '@kbn/core-analytics-browser';
import type { ExecutionContextSetup } from '@kbn/core-execution-context-browser';
import { ExecutionContextService } from './execution_context_service';
Expand All @@ -18,14 +20,19 @@ describe('ExecutionContextService', () => {
let curApp$: BehaviorSubject<string>;
let execService: ExecutionContextService;
let analytics: jest.Mocked<AnalyticsServiceSetup>;
let history: jest.Mocked<InternalApplicationStart['history']>;

beforeEach(() => {
analytics = analyticsServiceMock.createAnalyticsServiceSetup();
history = applicationServiceMock.createInternalStartContract().history as jest.Mocked<
InternalApplicationStart['history']
>;
execService = new ExecutionContextService();
execContext = execService.setup({ analytics });
curApp$ = new BehaviorSubject('app1');
execContext = execService.start({
curApp$,
history,
});
});

Expand Down Expand Up @@ -96,6 +103,50 @@ describe('ExecutionContextService', () => {
);
});

it('url updates automatically when there is a navigation', async () => {
execContext.set({
type: 'ghf',
meta: {
foo: 1,
},
description: 'first set',
});

expect(execContext.get()).toMatchInlineSnapshot(
{
name: 'app1',
description: 'first set',
type: 'ghf',
url: '/',
},
`
Object {
"description": "first set",
"meta": Object {
"foo": 1,
},
"name": "app1",
"type": "ghf",
"url": "/",
}
`
);

history.listen.mock.calls[0][0]({ ...history.location, pathname: '/another-path' }, 'PUSH');

expect(execContext.get()).toMatchInlineSnapshot(`
Object {
"description": "first set",
"meta": Object {
"foo": 1,
},
"name": "app1",
"type": "ghf",
"url": "/another-path",
}
`);
});

it('sets context and adds current url and appid when getting it', () => {
execContext.set({
type: 'ghf',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type {
ExecutionContextSetup,
ExecutionContextStart,
} from '@kbn/core-execution-context-browser';
import type { InternalApplicationStart } from '@kbn/core-application-browser-internal';

// Should be exported from elastic/apm-rum
export type LabelValue = string | number | boolean;
Expand All @@ -32,6 +33,7 @@ export interface SetupDeps {

export interface StartDeps {
curApp$: Observable<string | undefined>;
history: InternalApplicationStart['history'];
}

/** @internal */
Expand Down Expand Up @@ -75,7 +77,7 @@ export class ExecutionContextService
return this.contract;
}

public start({ curApp$ }: StartDeps) {
public start({ curApp$, history }: StartDeps) {
const start = this.contract!;

// Track app id changes and clear context on app change
Expand All @@ -86,6 +88,13 @@ export class ExecutionContextService
})
);

// Track URL changes to make sure that we reflect the new path name
this.subscription.add(
history.listen((location) => {
start.set({ url: location.pathname });
})
);

return start;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ export class CoreSystem {

const executionContext = this.executionContext.start({
curApp$: application.currentAppId$,
history: application.history,
});

const chrome = await this.chrome.start({
Expand Down

0 comments on commit 1810ac5

Please sign in to comment.