Skip to content

Commit

Permalink
Get current route for wc client api(#3780)
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannesDoberer authored Jun 25, 2024
1 parent 7948985 commit 5f4e776
Show file tree
Hide file tree
Showing 12 changed files with 399 additions and 2 deletions.
11 changes: 11 additions & 0 deletions container/cypress/e2e/test-app/wc/wc-container.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,16 @@ describe('Web Container Test', () => {
expect(stub.getCall(0)).to.be.calledWith('LuigiClient.getAnchor()="testanchor"');
});
});
it('LuigiClient API getCurrentRoute for LuigiContainer', () => {
const stub = cy.stub();
cy.on('window:alert', stub);
cy.get('[data-test-id="luigi-client-api-test-01"]')
.shadow()
.contains('getCurrentRoute')
.click()
.then(() => {
expect(stub.getCall(0)).to.be.calledWith('current route: /wc/clientAPI.html');
});
});
});
});
19 changes: 18 additions & 1 deletion container/src/services/webcomponents.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,12 @@ export class WebComponentService {
let fromContext = null;
let fromClosestContext = false;
let fromVirtualTreeRoot = false;
let fromParent = false;
let nodeParams = {};

const linkManagerInstance = {
navigate: (route , settings = {})=> {
const options = { fromContext, fromClosestContext, fromVirtualTreeRoot, nodeParams, ...settings };
const options = { fromContext, fromClosestContext, fromVirtualTreeRoot, fromParent, nodeParams, ...settings };
this.dispatchLuigiEvent(Events.NAVIGATION_REQUEST, { link: route , ...options});
},
fromClosestContext: () => {
Expand All @@ -111,6 +112,22 @@ export class WebComponentService {
fromVirtualTreeRoot = true;
return linkManagerInstance;
},
fromParent:()=>{
fromParent = true;
return linkManagerInstance;
},
getCurrentRoute:()=>{
const options = { fromContext, fromClosestContext, fromVirtualTreeRoot, fromParent, nodeParams };
return new Promise((resolve, reject) => {
this.containerService.dispatch(Events.GET_CURRENT_ROUTE_REQUEST, this.thisComponent,{ ...options }, (route)=>{
if(route){
resolve(route);
}else{
reject('No current route received.');
}
}, 'callback');
});
},
withParams: (params) => {
nodeParams = params;
return linkManagerInstance;
Expand Down
4 changes: 4 additions & 0 deletions container/test-app/wc/clientAPI.html
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ <h3>
luigiContainer.addEventListener(MFEventID.SET_VIEW_GROUP_DATA_REQUEST, event => {
console.log('Set View Group Data Request received', event.detail, event);
});

luigiContainer.addEventListener(MFEventID.GET_CURRENT_ROUTE_REQUEST, event => {
event.callback(window.location.pathname);
});
});
</script>
</body>
Expand Down
14 changes: 14 additions & 0 deletions container/test-app/wc/helloWorldWC.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ export default class extends HTMLElement {
const getDirtyStatusBtn = document.createElement('template');
getDirtyStatusBtn.innerHTML = '<button id="getDirtyStatus">getDirtyStatus</button>';

const getCurrentRouteBtn = document.createElement('template');
getCurrentRouteBtn.innerHTML = '<button id="getCurrentRoute">getCurrentRoute</button>';

const uxManagerMultipleRequestsBtn = document.createElement('template');
uxManagerMultipleRequestsBtn.innerHTML = `<button id="uxManagerManyRequests">uxManager().closeUserSettings,
openUserSettings,
Expand Down Expand Up @@ -97,6 +100,7 @@ export default class extends HTMLElement {
this._shadowRoot.appendChild(linkManagerOpenAsRequestsBtn.content.cloneNode(true));
this._shadowRoot.appendChild(linkManagerUpdateTopPathExistsBackBtn.content.cloneNode(true));
this._shadowRoot.appendChild(setViewGroupDataBtn.content.cloneNode(true));
this._shadowRoot.appendChild(getCurrentRouteBtn.content.cloneNode(true));

this._shadowRoot.appendChild(empty.content.cloneNode(true));

Expand Down Expand Up @@ -260,6 +264,16 @@ export default class extends HTMLElement {
this.$setViewGroupData.addEventListener('click', () => {
this.LuigiClient.setViewGroupData({ vg: 'some data' });
});

this.$getCurrentRoute = this._shadowRoot.querySelector('#getCurrentRoute');
this.$getCurrentRoute.addEventListener('click', () => {
this.LuigiClient.linkManager()
.getCurrentRoute()
.then(result => {
console.log(result);
alert('current route: ' + result);
});
});
}

set context(ctx) {
Expand Down
70 changes: 70 additions & 0 deletions container/test/services/webcomponents.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ describe('createClientAPI', () => {
// assert
const expectedPayload = {
fromClosestContext: false,
fromParent: false,
fromContext: null,
fromVirtualTreeRoot: false,
link: "/test/route",
Expand All @@ -176,6 +177,7 @@ describe('createClientAPI', () => {
// assert
const expectedPayload = {
fromClosestContext: false,
fromParent: false,
fromContext: null,
fromVirtualTreeRoot: false,
link: "/test/route",
Expand All @@ -201,6 +203,7 @@ describe('createClientAPI', () => {
// assert
const expectedPayload = {
fromClosestContext: false,
fromParent: false,
fromContext: null,
fromVirtualTreeRoot: false,
link: "/test/route",
Expand All @@ -227,6 +230,7 @@ describe('createClientAPI', () => {
// assert
const expectedPayload = {
fromClosestContext: false,
fromParent: false,
fromContext: null,
fromVirtualTreeRoot: false,
link: "/test/route",
Expand Down Expand Up @@ -254,6 +258,7 @@ describe('createClientAPI', () => {
// assert
const expectedPayload = {
fromClosestContext: true,
fromParent: false,
fromContext: null,
fromVirtualTreeRoot: false,
link: "/test/route",
Expand All @@ -276,6 +281,7 @@ describe('createClientAPI', () => {
// assert
const expectedPayload = {
fromClosestContext: false,
fromParent: false,
fromContext: {test: 'data'},
fromVirtualTreeRoot: false,
link: "/test/route",
Expand All @@ -298,13 +304,76 @@ describe('createClientAPI', () => {
// assert
const expectedPayload = {
fromClosestContext: false,
fromParent: false,
fromContext: null,
fromVirtualTreeRoot: true,
link: "/test/route",
nodeParams: {}
};
expect(dispatchEventSpy).toHaveBeenCalledWith(Events.NAVIGATION_REQUEST, expectedPayload);
});

it('test linkManager currentRoute', () => {

// Mock and spy on functions
service.containerService.dispatch = jest.fn((event, component, options, callback) => {
callback('/current/route');
});

// act
const clientAPI = service.createClientAPI(undefined, 'nodeId', 'wc_id', 'component');
const currentRoutePromise = clientAPI.linkManager().getCurrentRoute();

// assert
const expectedPayload = {
fromClosestContext: false,
fromParent: false,
fromContext: null,
fromVirtualTreeRoot: false,
nodeParams: {}
};
return currentRoutePromise.then(result => {
expect(service.containerService.dispatch).toHaveBeenCalledWith(
Events.GET_CURRENT_ROUTE_REQUEST,
service.thisComponent,
expectedPayload,
expect.any(Function),
'callback'
);
expect(result).toBe('/current/route');
});
});

it('test linkManager currentRoute from parent', () => {

// Mock and spy on functions
service.containerService.dispatch = jest.fn((event, component, options, callback) => {
callback('/route');
});

// act
const clientAPI = service.createClientAPI(undefined, 'nodeId', 'wc_id', 'component');
const currentRoutePromise = clientAPI.linkManager().fromParent().getCurrentRoute();

// assert
const expectedPayload = {
fromClosestContext: false,
fromParent: true,
fromContext: null,
fromVirtualTreeRoot: false,
nodeParams: {}
};
return currentRoutePromise.then(result => {
expect(service.containerService.dispatch).toHaveBeenCalledWith(
Events.GET_CURRENT_ROUTE_REQUEST,
service.thisComponent,
expectedPayload,
expect.any(Function),
'callback'
);
expect(result).toBe('/route');
});
});

it('test linkManager withParams', () => {
const route = '/test/route'
Expand All @@ -321,6 +390,7 @@ describe('createClientAPI', () => {
const expectedPayload = {
fromClosestContext: false,
fromContext: null,
fromParent: false,
fromVirtualTreeRoot: false,
link: "/test/route",
nodeParams: {params: 'some params'}
Expand Down
2 changes: 1 addition & 1 deletion core/src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -1103,7 +1103,7 @@
* @param params {Object} navigation options
* @returns {string} the path built
*/
const buildPathForGetCurrentRoute = params => {
export const buildPathForGetCurrentRoute = params => {
let localNavPath = navigationPath;
if (currentNode) {
let parent = currentNode.parent;
Expand Down
19 changes: 19 additions & 0 deletions core/src/core-api/_internalLinkManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export class linkManager extends LuigiCoreAPIBase {
nodeParams: {},
errorSkipNavigation: false,
fromContext: null,
fromParent: false,
fromClosestContext: false,
relative: false,
link: ''
Expand Down Expand Up @@ -87,13 +88,23 @@ export class linkManager extends LuigiCoreAPIBase {
fromClosestContext() {
this.options.fromContext = null;
this.options.fromClosestContext = true;
this.options.fromParent = false;
return this;
}

fromVirtualTreeRoot() {
this.options.fromContext = null;
this.options.fromClosestContext = false;
this.options.fromVirtualTreeRoot = true;
this.options.fromParent = false;
return this;
}

fromParent() {
this.options.fromContext = null;
this.options.fromClosestContext = false;
this.options.fromVirtualTreeRoot = false;
this.options.fromParent = true;
return this;
}

Expand Down Expand Up @@ -125,6 +136,14 @@ export class linkManager extends LuigiCoreAPIBase {
});
}

/**
* Retrieves the current route.
* @returns The current route by invoking the buildPathForGetCurrentRoute method from App.svelte, which is assigned to the _app property of the Luigi object.
*/
getCurrentRoute() {
return Luigi._app.ctx[Luigi._app.props.buildPathForGetCurrentRoute](this.options);
}

sendPostMessageToLuigiCore(msg) {
window.postMessage(msg, '*');
}
Expand Down
26 changes: 26 additions & 0 deletions core/src/core-api/navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,32 @@ class LuigiNavigationManager {
return new linkManager().fromVirtualTreeRoot();
}

/**
* Enables navigating to sibling nodes without knowing the absolute path.
* @memberof LuigiNavigation
* @returns {linkManager} link manager instance
* @since NEXTRELEASE
* @example
* Luigi.navigation().fromParent().navigate('/sibling')
*/
fromParent() {
return new linkManager().fromParent();
}

/**
* Gets the Luigi route associated with the current micro frontend.
* @memberof LuigiNavigation
* @returns a String value specifying the current Luigi route
* @since NEXTRELEASE
* @example
* Luigi.navigation().getCurrentRoute();
* Luigi.navigation().fromContext('project').getCurrentRoute();
* Luigi.navigation().fromVirtualTreeRoot().getCurrentRoute();
*/
getCurrentRoute() {
return new linkManager().getCurrentRoute();
}

/**
* Sends node parameters to the route. The parameters are used by the `navigate` function. Use it optionally in combination with any of the navigation functions and receive it as part of the context object in Luigi Client.
* @memberof LuigiNavigation
Expand Down
2 changes: 2 additions & 0 deletions core/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ const configReadyCallback = () => {
return app.getDirtyStatus();
};

//adding App.svelte to a internal Luigi variable
Luigi._app = app.$$;
resolve();
});
});
Expand Down
34 changes: 34 additions & 0 deletions docs/luigi-core-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,40 @@ Returns **linkManager** link manager instance

- **since**: 1.0.1

#### fromParent

Enables navigating to sibling nodes without knowing the absolute path.

##### Examples

```javascript
Luigi.navigation().fromParent().navigate('/sibling')
```

Returns **linkManager** link manager instance

**Meta**

- **since**: NEXTRELEASE

#### getCurrentRoute

Gets the Luigi route associated with the current micro frontend.

##### Examples

```javascript
Luigi.navigation().getCurrentRoute();
Luigi.navigation().fromContext('project').getCurrentRoute();
Luigi.navigation().fromVirtualTreeRoot().getCurrentRoute();
```

Returns **any** a String value specifying the current Luigi route

**Meta**

- **since**: NEXTRELEASE

#### withParams

Sends node parameters to the route. The parameters are used by the `navigate` function. Use it optionally in combination with any of the navigation functions and receive it as part of the context object in Luigi Client.
Expand Down
Loading

0 comments on commit 5f4e776

Please sign in to comment.