Skip to content

Commit

Permalink
Introduce clearNavigationCache() to clear children and titleResolver …
Browse files Browse the repository at this point in the history
…cache (SAP#2383)

* Clear children and titleResolver cache

* Update the description of clearNavigationCache()
  • Loading branch information
stanleychh authored Nov 18, 2021
1 parent e976a37 commit d48229a
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 37 deletions.
27 changes: 27 additions & 0 deletions core/src/core-api/config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { AsyncHelpers, EventListenerHelpers, GenericHelpers, StateHelpers } from '../utilities/helpers';
import { LuigiAuth, LuigiElements } from '.';
import { AuthLayerSvc, LifecycleHooks } from '../services';
import { NodeDataManagementStorage } from '../services/node-data-management.js';
/**
* @name Configuration
*/
Expand Down Expand Up @@ -279,6 +280,32 @@ class LuigiConfig {
this.unload();
this.setConfig(cfg);
}

/**
* Clear navigation node related caches.
* @memberof Configuration
* @example
* Luigi.clearNavigationCache();
* @since NEXT_RELEASE
*/
clearNavigationCache() {
NodeDataManagementStorage.deleteCache();

const clearTitleResolverCache = nodes => {
if (nodes && nodes.forEach) {
nodes.forEach(node => {
if (node.titleResolver && node.titleResolver._cache) {
node.titleResolver._cache = undefined;
}
if (node.children) {
clearTitleResolverCache(node.children);
}
});
}
};

clearTitleResolverCache(this.getConfig().navigation.nodes);
}
}

export const config = new LuigiConfig();
66 changes: 33 additions & 33 deletions core/test/services/config.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ describe('Config', () => {

// then
assert.equal(LuigiConfig.getConfigBooleanValue('whatever'), false);
assert.equal(
LuigiConfig.getConfigBooleanValue('whateverparent.whateverchild'),
false
);
assert.equal(LuigiConfig.getConfigBooleanValue('whateverparent.whateverchild'), false);
});
});

Expand All @@ -51,42 +48,28 @@ describe('Config', () => {
//given
LuigiConfig.config = {
truthyFn: (param1, param2) => 'value' + param1 + param2,
truthyFnAsync: (param1, param2) =>
Promise.resolve('value' + param1 + param2)
truthyFnAsync: (param1, param2) => Promise.resolve('value' + param1 + param2)
};

const resultTruthyFn = await LuigiConfig.executeConfigFnAsync(
'truthyFn',
false,
'foo',
'bar'
);
const resultTruthyFn = await LuigiConfig.executeConfigFnAsync('truthyFn', false, 'foo', 'bar');
assert.equal(resultTruthyFn, 'valuefoobar');

const resultTruthyFnAsync = await LuigiConfig.executeConfigFnAsync(
'truthyFnAsync',
false,
'foo',
'bar'
);
const resultTruthyFnAsync = await LuigiConfig.executeConfigFnAsync('truthyFnAsync', false, 'foo', 'bar');
assert.equal(resultTruthyFnAsync, 'valuefoobar');
});

it('returns undefined on non-existing fn', async () => {
//given
LuigiConfig.config = {};

const resultUndefined = await LuigiConfig.executeConfigFnAsync(
'not.existing.value'
);
const resultUndefined = await LuigiConfig.executeConfigFnAsync('not.existing.value');
assert.isUndefined(resultUndefined, 'async fn result is not undefined');
});

it('returns undefined on error/rejected promise', async () => {
//given
LuigiConfig.config = {
rejectFnAsync: (param1, param2) =>
Promise.reject(new Error('rejected')),
rejectFnAsync: (param1, param2) => Promise.reject(new Error('rejected')),
errFnAsync: (param1, param2) => {
throw new Error({
name: 'someError',
Expand All @@ -95,24 +78,18 @@ describe('Config', () => {
}
};

const resultRejectFnAsync = await LuigiConfig.executeConfigFnAsync(
'rejectFnAsync'
);
const resultRejectFnAsync = await LuigiConfig.executeConfigFnAsync('rejectFnAsync');
assert.isUndefined(resultRejectFnAsync, 'rejection did not return');

const resultErrFnAsync = await LuigiConfig.executeConfigFnAsync(
'errFnAsync'
);
const resultErrFnAsync = await LuigiConfig.executeConfigFnAsync('errFnAsync');
assert.isUndefined(resultErrFnAsync, 'error did not return');
});

it('throws an error if throwError flag is set', async () => {
sinon
.stub(AsyncHelpers, 'applyFunctionPromisified')
.returns(Promise.reject(new Error('rejected')));
sinon.stub(AsyncHelpers, 'applyFunctionPromisified').returns(Promise.reject(new Error('rejected')));
//given
LuigiConfig.config = {
rejectFnAsync: (param1, param2) => { }
rejectFnAsync: (param1, param2) => {}
};

// second parameter throws an error and we want it to throw an error on failure
Expand Down Expand Up @@ -179,4 +156,27 @@ describe('Config', () => {
sinon.assert.calledOnce(console.log);
});
});

describe('clearNavigationCache', () => {
it('should clean navigation titleResolver cache', () => {
LuigiConfig.config = {
navigation: {
nodes: {
titleResolver: {
url: 'http://localhost',
_cache: {
key: 'cacheKey',
value: 'cacheValue'
}
}
}
}
};
LuigiConfig.clearNavigationCache();

const actual = LuigiConfig.getConfigValue('navigation.titleResolver._cache');
const expected = undefined;
assert.equal(actual, expected);
});
});
});
3 changes: 1 addition & 2 deletions core/test/utilities/helpers/navigation-helpers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const chai = require('chai');
const assert = chai.assert;
const sinon = require('sinon');
import { AuthHelpers, NavigationHelpers, GenericHelpers, RoutingHelpers } from '../../../src/utilities/helpers';
import { AuthHelpers, NavigationHelpers, RoutingHelpers } from '../../../src/utilities/helpers';
import { LuigiAuth, LuigiConfig } from '../../../src/core-api';
import { Routing } from '../../../src/services/routing';
import { Navigation } from '../../../src/navigation/services/navigation';
Expand Down Expand Up @@ -490,7 +490,6 @@ describe('Navigation-helpers', () => {
it('group nodes by category label', () => {
nodes[1].category.collapsible = true;
const result = NavigationHelpers.groupNodesBy(nodes, 'category', true);
console.log('result ', result.test['metaInfo']);
assert.deepEqual(Object.keys(result), ['1', 'test', 'luigi']);
assert.deepEqual(result.test['metaInfo'], { categoryUid: 'test', label: 'test', collapsible: true, order: 1 });
});
Expand Down
4 changes: 2 additions & 2 deletions docs/luigi-client-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -453,11 +453,11 @@ LuigiClient.linkManager().newTab().navigate('/projects/xy/foobar');

#### preserveQueryParams

Keeps url's query parameters for a navigation request.
Keeps the URL's query parameters for a navigation request.

##### Parameters

- `preserve` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** By default, it is set to `false`. If it is set to `true` the url's query parameters will be kept after navigation. (optional, default `false`)
- `preserve` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** By default, it is set to `false`. If it is set to `true`, the URL's query parameters will be kept after navigation. (optional, default `false`)

##### Examples

Expand Down
14 changes: 14 additions & 0 deletions docs/luigi-core-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,20 @@ Luigi.reset();

- **since**: 1.14.0

#### clearNavigationCache

Clear navigation node related caches.

##### Examples

```javascript
Luigi.clearNavigationCache();
```

**Meta**

- **since**: NEXT_RELEASE

## Luigi.elements()

<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
Expand Down

0 comments on commit d48229a

Please sign in to comment.