tests(build): add loader and setter tests

This commit is contained in:
Sam Tolmay 2020-10-21 13:48:58 +02:00
parent 26c70dbc09
commit cefbb69d9c
7 changed files with 102 additions and 5 deletions

1
.gitignore vendored
View File

@ -9,3 +9,4 @@
packages/express/config/**
packages/build/src/test/writeFile.txt
packages/build/src/utils/files/writeFile.test.js

View File

@ -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;

View 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'
);
});

View 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
}
});

View File

@ -0,0 +1 @@
File loader text file 1.

View File

@ -0,0 +1 @@
File loader text file 2.

View File

@ -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);