diff --git a/packages/api/src/context/readConfigFile.test.js b/packages/api/src/context/readConfigFile.test.js new file mode 100644 index 000000000..26db8d884 --- /dev/null +++ b/packages/api/src/context/readConfigFile.test.js @@ -0,0 +1,33 @@ +/* + Copyright 2020-2021 Lowdefy, Inc + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +import { getFileExtension } from '@lowdefy/node-utils'; + +jest.mock('@lowdefy/node-utils', () => { + return { + getFileExtension, + readFile: jest.fn(), + }; +}); + +test('readConfigFile', async () => { + const nodeUtils = await import('@lowdefy/node-utils'); + nodeUtils.mockImplementation(() => Promise.resove('config value')); + const createReadConfigFile = (await import('./readConfigFile.js')).default; + const readConfigFile = createReadConfigFile({ buildDirectory: '/build' }); + const res = await readConfigFile('file'); + expect(res).toEqual('config value'); +}); diff --git a/packages/utils/helpers/src/cachedPromises.js b/packages/utils/helpers/src/cachedPromises.js index 63de5868d..1a56d46b6 100644 --- a/packages/utils/helpers/src/cachedPromises.js +++ b/packages/utils/helpers/src/cachedPromises.js @@ -14,10 +14,10 @@ limitations under the License. */ -function cachedPromises(getter) { +function createCachedPromises(getter) { const cache = new Map(); - function getCachedPromise(key) { + function cachedPromises(key) { if (cache.has(key)) { return Promise.resolve(cache.get(key)); } @@ -26,7 +26,7 @@ function cachedPromises(getter) { return Promise.resolve(promise); } - return getCachedPromise; + return cachedPromises; } -export default cachedPromises; +export default createCachedPromises; diff --git a/packages/utils/helpers/src/cachedPromises.test.js b/packages/utils/helpers/src/cachedPromises.test.js new file mode 100644 index 000000000..38d44243e --- /dev/null +++ b/packages/utils/helpers/src/cachedPromises.test.js @@ -0,0 +1,52 @@ +/* + Copyright 2020-2021 Lowdefy, Inc + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +import createCachedPromises from './cachedPromises.js'; +import wait from './wait.js'; + +test('cachedPromises calls getter with key', async () => { + const getter = jest.fn(() => Promise.resolve('value')); + const cachedGetter = createCachedPromises(getter); + const promise = cachedGetter('key'); + expect(`${promise}`).toEqual('[object Promise]'); + const result = await promise; + expect(result).toEqual('value'); + expect(getter.mock.calls).toEqual([['key']]); +}); + +test('cachedPromises only calls getter once', async () => { + const getter = jest.fn(() => Promise.resolve('value')); + const cachedGetter = createCachedPromises(getter); + const result1 = await cachedGetter('key'); + expect(result1).toEqual('value'); + const result2 = await cachedGetter('key'); + expect(result2).toEqual('value'); + expect(getter.mock.calls).toEqual([['key']]); +}); + +test('getter is called once if first call has not yet resolved', async () => { + const getter = jest.fn(async () => { + await wait(10); + return 'value'; + }); + const cachedGetter = createCachedPromises(getter); + const promise1 = cachedGetter('key'); + const promise2 = cachedGetter('key'); + expect(getter.mock.calls).toEqual([['key']]); + await promise1; + await promise2; + expect(getter.mock.calls).toEqual([['key']]); +});