mirror of
https://github.com/lowdefy/lowdefy.git
synced 2025-03-19 15:01:06 +08:00
tests(build): add loader and setter tests
This commit is contained in:
parent
26c70dbc09
commit
cefbb69d9c
1
.gitignore
vendored
1
.gitignore
vendored
@ -9,3 +9,4 @@
|
||||
|
||||
packages/express/config/**
|
||||
packages/build/src/test/writeFile.txt
|
||||
packages/build/src/utils/files/writeFile.test.js
|
||||
|
@ -21,13 +21,14 @@ import getFile from '../utils/files/getFile';
|
||||
|
||||
function createFileBatchLoader({ baseDir }) {
|
||||
async function loader(keys) {
|
||||
const filePaths = keys.map((key) => path.resolve(baseDir, key));
|
||||
const fetched = [];
|
||||
const promises = keys.map(async (filePath) => {
|
||||
const item = await getFile(path.resolve(baseDir, filePath));
|
||||
const promises = filePaths.map(async (filePath) => {
|
||||
const item = await getFile(filePath);
|
||||
fetched.push(item);
|
||||
});
|
||||
await Promise.all(promises);
|
||||
const returned = keys
|
||||
const returned = filePaths
|
||||
.map((filePath) =>
|
||||
fetched.find((item) => {
|
||||
return get(item, 'filePath') === filePath;
|
||||
|
51
packages/build/src/context/fileLoader.test.js
Normal file
51
packages/build/src/context/fileLoader.test.js
Normal file
@ -0,0 +1,51 @@
|
||||
/*
|
||||
Copyright 2020 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 path from 'path';
|
||||
import createFileLoader from './fileLoader';
|
||||
|
||||
const baseDir = path.resolve(process.cwd(), 'src/test/fileLoader');
|
||||
|
||||
test('load file', async () => {
|
||||
const fileLoader = createFileLoader({ baseDir });
|
||||
const res = await fileLoader.load('fileLoader1.txt');
|
||||
expect(res).toEqual('File loader text file 1.');
|
||||
});
|
||||
|
||||
test('load two files', async () => {
|
||||
const fileLoader = createFileLoader({ baseDir });
|
||||
const files = ['fileLoader1.txt', 'fileLoader2.txt'];
|
||||
const res = await Promise.all(files.map((file) => fileLoader.load(file)));
|
||||
expect(res).toEqual(['File loader text file 1.', 'File loader text file 2.']);
|
||||
});
|
||||
|
||||
test('load two files, one file errors', async () => {
|
||||
const fileLoader = createFileLoader({ baseDir });
|
||||
const files = ['fileLoader1.txt', 'doesNotExist.txt'];
|
||||
await expect(Promise.all(files.map((file) => fileLoader.load(file)))).rejects.toThrow(
|
||||
'src/test/fileLoader/doesNotExist.txt", but file does not exist'
|
||||
);
|
||||
});
|
||||
|
||||
test('load file, file does not exist', async () => {
|
||||
const filePath = path.resolve(baseDir, 'doesNotExist.txt');
|
||||
const fileLoader = createFileLoader({ baseDir });
|
||||
// Since error message contains exact file path, test if parts of error message are present
|
||||
await expect(fileLoader.load(filePath)).rejects.toThrow('Tried to read file with file path');
|
||||
await expect(fileLoader.load(filePath)).rejects.toThrow(
|
||||
'src/test/fileLoader/doesNotExist.txt", but file does not exist'
|
||||
);
|
||||
});
|
43
packages/build/src/context/fileSetter.test.js
Normal file
43
packages/build/src/context/fileSetter.test.js
Normal file
@ -0,0 +1,43 @@
|
||||
/*
|
||||
Copyright 2020 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 fs from 'fs';
|
||||
import path from 'path';
|
||||
import createFileSetter from './fileSetter';
|
||||
|
||||
const baseDir = path.resolve(process.cwd(), 'src/test/fileSetter');
|
||||
|
||||
test('writeFile', async () => {
|
||||
const filePath = path.resolve(baseDir, 'writeFile.txt');
|
||||
try {
|
||||
fs.unlinkSync(filePath);
|
||||
} catch (error) {
|
||||
//pass
|
||||
}
|
||||
expect(fs.existsSync(filePath)).toBe(false);
|
||||
const fileSetter = createFileSetter({ baseDir });
|
||||
await fileSetter.set({
|
||||
filePath: 'writeFile.txt',
|
||||
content: 'Test fileSetter file',
|
||||
});
|
||||
const res = fs.readFileSync(filePath, 'utf8');
|
||||
expect(res).toEqual('Test fileSetter file');
|
||||
try {
|
||||
fs.unlinkSync(filePath);
|
||||
} catch (error) {
|
||||
//pass
|
||||
}
|
||||
});
|
1
packages/build/src/test/fileLoader/fileLoader1.txt
Normal file
1
packages/build/src/test/fileLoader/fileLoader1.txt
Normal file
@ -0,0 +1 @@
|
||||
File loader text file 1.
|
1
packages/build/src/test/fileLoader/fileLoader2.txt
Normal file
1
packages/build/src/test/fileLoader/fileLoader2.txt
Normal file
@ -0,0 +1 @@
|
||||
File loader text file 2.
|
@ -17,7 +17,6 @@
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import writeFile from './writeFile';
|
||||
import readFile from './readFile';
|
||||
|
||||
const baseDir = path.resolve(process.cwd(), 'src/test/getFile');
|
||||
|
||||
@ -33,7 +32,7 @@ test('writeFile', async () => {
|
||||
filePath,
|
||||
content: `Test Write File`,
|
||||
});
|
||||
const res = await readFile(filePath);
|
||||
const res = fs.readFileSync(filePath, 'utf8');
|
||||
expect(res).toEqual(`Test Write File`);
|
||||
try {
|
||||
fs.unlinkSync(filePath);
|
||||
|
Loading…
x
Reference in New Issue
Block a user