mirror of
https://github.com/lowdefy/lowdefy.git
synced 2025-03-19 15:01:06 +08:00
fix: Add missing api and helpers tests.
This commit is contained in:
parent
e3f3ad269f
commit
aa1d72c191
33
packages/api/src/context/readConfigFile.test.js
Normal file
33
packages/api/src/context/readConfigFile.test.js
Normal file
@ -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');
|
||||
});
|
@ -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;
|
||||
|
52
packages/utils/helpers/src/cachedPromises.test.js
Normal file
52
packages/utils/helpers/src/cachedPromises.test.js
Normal file
@ -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']]);
|
||||
});
|
Loading…
x
Reference in New Issue
Block a user