mirror of
https://github.com/lowdefy/lowdefy.git
synced 2025-01-24 13:45:07 +08:00
tests(build): clean up tests
This commit is contained in:
parent
fc6176d312
commit
26c70dbc09
2
packages/build/src/test/getFile/html.html
Normal file
2
packages/build/src/test/getFile/html.html
Normal file
@ -0,0 +1,2 @@
|
||||
<h1>HTML Heading</h1>
|
||||
<p>Paragraph</p>
|
3
packages/build/src/test/getFile/markdown.md
Normal file
3
packages/build/src/test/getFile/markdown.md
Normal file
@ -0,0 +1,3 @@
|
||||
### Markdown title
|
||||
|
||||
Markdown body
|
@ -1,3 +1 @@
|
||||
Hello
|
||||
|
||||
This is a txt file.
|
@ -1,2 +0,0 @@
|
||||
<h1>Heading</h1>
|
||||
<p>Hello there</p>
|
@ -1,3 +0,0 @@
|
||||
### Title
|
||||
|
||||
Hello there
|
@ -1,17 +0,0 @@
|
||||
/*
|
||||
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.
|
||||
*/
|
||||
|
||||
Test File
|
1
packages/build/src/test/readFile/readFile.txt
Normal file
1
packages/build/src/test/readFile/readFile.txt
Normal file
@ -0,0 +1 @@
|
||||
Test Read File
|
@ -1,3 +1,19 @@
|
||||
/*
|
||||
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.
|
||||
*/
|
||||
|
||||
function testContext({ artifactSetter, configLoader, logger = {}, metaLoader } = {}) {
|
||||
const defaultLogger = {
|
||||
success: () => {},
|
||||
|
1
packages/build/src/test/writeFile/writeFile.txt
Normal file
1
packages/build/src/test/writeFile/writeFile.txt
Normal file
@ -0,0 +1 @@
|
||||
Test Write File
|
@ -17,8 +17,10 @@
|
||||
import path from 'path';
|
||||
import getFile from './getFile';
|
||||
|
||||
const baseDir = path.resolve(process.cwd(), 'src/test/getFile');
|
||||
|
||||
test('getFile jsonFile.json', async () => {
|
||||
const filePath = path.resolve(process.cwd(), 'src/test/jsonFile.json');
|
||||
const filePath = path.resolve(baseDir, 'jsonFile.json');
|
||||
const file = await getFile(filePath);
|
||||
expect(file).toEqual({
|
||||
filePath,
|
||||
@ -30,7 +32,7 @@ test('getFile jsonFile.json', async () => {
|
||||
});
|
||||
|
||||
test('getFile yamlFile.yaml', async () => {
|
||||
const filePath = path.resolve(process.cwd(), 'src/test/yamlFile.yaml');
|
||||
const filePath = path.resolve(baseDir, 'yamlFile.yaml');
|
||||
const file = await getFile(filePath);
|
||||
expect(file).toEqual({
|
||||
filePath,
|
||||
@ -42,7 +44,7 @@ test('getFile yamlFile.yaml', async () => {
|
||||
});
|
||||
|
||||
test('getFile ymlFile.yml', async () => {
|
||||
const filePath = path.resolve(process.cwd(), 'src/test/ymlFile.yml');
|
||||
const filePath = path.resolve(baseDir, 'ymlFile.yml');
|
||||
const file = await getFile(filePath);
|
||||
expect(file).toEqual({
|
||||
filePath,
|
||||
@ -53,12 +55,44 @@ test('getFile ymlFile.yml', async () => {
|
||||
});
|
||||
});
|
||||
|
||||
test('getFile markdown.md', async () => {
|
||||
const filePath = path.resolve(baseDir, 'markdown.md');
|
||||
const file = await getFile(filePath);
|
||||
expect(file).toEqual({
|
||||
filePath,
|
||||
content: `### Markdown title
|
||||
|
||||
Markdown body
|
||||
`,
|
||||
});
|
||||
});
|
||||
|
||||
test('getFile html.html', async () => {
|
||||
const filePath = path.resolve(baseDir, 'html.html');
|
||||
const file = await getFile(filePath);
|
||||
expect(file).toEqual({
|
||||
filePath,
|
||||
content: `<h1>HTML Heading</h1>
|
||||
<p>Paragraph</p>
|
||||
`,
|
||||
});
|
||||
});
|
||||
|
||||
test('getFile text.txt', async () => {
|
||||
const filePath = path.resolve(baseDir, 'text.txt');
|
||||
const file = await getFile(filePath);
|
||||
expect(file).toEqual({
|
||||
filePath,
|
||||
content: `This is a txt file.`,
|
||||
});
|
||||
});
|
||||
|
||||
test('getFile doesNotExist.txt', async () => {
|
||||
const filePath = path.resolve(process.cwd(), 'src/test/doesNotExist.txt');
|
||||
const filePath = path.resolve(baseDir, 'doesNotExist.txt');
|
||||
// Since error message contains exact file path, test if parts of error message are present
|
||||
await expect(getFile(filePath)).rejects.toThrow('Tried to read file with file path');
|
||||
await expect(getFile(filePath)).rejects.toThrow(
|
||||
'src/test/doesNotExist.txt", but file does not exist'
|
||||
'src/test/getFile/doesNotExist.txt", but file does not exist'
|
||||
);
|
||||
});
|
||||
|
||||
@ -67,37 +101,3 @@ test('getFile null', async () => {
|
||||
'Tried to read file with file path null, but file path should be a string'
|
||||
);
|
||||
});
|
||||
|
||||
test('getFile markdown.md', async () => {
|
||||
const filePath = path.resolve(process.cwd(), 'src/test/markdown.md');
|
||||
const file = await getFile(filePath);
|
||||
expect(file).toEqual({
|
||||
filePath,
|
||||
content: `### Title
|
||||
|
||||
Hello there
|
||||
`,
|
||||
});
|
||||
});
|
||||
|
||||
test('getFile html.html', async () => {
|
||||
const filePath = path.resolve(process.cwd(), 'src/test/html.html');
|
||||
const file = await getFile(filePath);
|
||||
expect(file).toEqual({
|
||||
filePath,
|
||||
content: `<h1>Heading</h1>
|
||||
<p>Hello there</p>
|
||||
`,
|
||||
});
|
||||
});
|
||||
|
||||
test('getFile text.txt', async () => {
|
||||
const filePath = path.resolve(process.cwd(), 'src/test/text.txt');
|
||||
const file = await getFile(filePath);
|
||||
expect(file).toEqual({
|
||||
filePath,
|
||||
content: `Hello
|
||||
|
||||
This is a txt file.`,
|
||||
});
|
||||
});
|
||||
|
@ -16,39 +16,25 @@
|
||||
import path from 'path';
|
||||
import readFile from './readFile';
|
||||
|
||||
const baseDir = path.resolve(process.cwd(), 'src/test/readFile');
|
||||
|
||||
test('readFile', async () => {
|
||||
const filePath = path.resolve(process.cwd(), 'src/test/readFile.txt');
|
||||
const filePath = path.resolve(baseDir, 'readFile.txt');
|
||||
const res = await readFile(filePath);
|
||||
expect(res).toEqual(`/*
|
||||
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.
|
||||
*/
|
||||
|
||||
Test File`);
|
||||
expect(res).toEqual(`Test Read File`);
|
||||
});
|
||||
|
||||
test('readFile file not found throws', async () => {
|
||||
const filePath = path.resolve(process.cwd(), 'src/test/doesNotExist.txt');
|
||||
const filePath = path.resolve(baseDir, 'doesNotExist.txt');
|
||||
// Since error message contains exact file path, test if parts of error message are present
|
||||
await expect(readFile(filePath)).rejects.toThrow('Tried to read file with file path');
|
||||
await expect(readFile(filePath)).rejects.toThrow(
|
||||
'src/test/doesNotExist.txt", but file does not exist'
|
||||
'src/test/readFile/doesNotExist.txt", but file does not exist'
|
||||
);
|
||||
});
|
||||
|
||||
test('readFile error', async () => {
|
||||
const filePath = path.resolve(process.cwd(), 'src/test/');
|
||||
const filePath = baseDir;
|
||||
await expect(readFile(filePath)).rejects.toThrow(
|
||||
'EISDIR: illegal operation on a directory, read'
|
||||
);
|
||||
|
@ -19,8 +19,10 @@ import path from 'path';
|
||||
import writeFile from './writeFile';
|
||||
import readFile from './readFile';
|
||||
|
||||
const baseDir = path.resolve(process.cwd(), 'src/test/getFile');
|
||||
|
||||
test('writeFile', async () => {
|
||||
const filePath = path.resolve(process.cwd(), 'src/test/writeFile.txt');
|
||||
const filePath = path.resolve(baseDir, 'writeFile.txt');
|
||||
try {
|
||||
fs.unlinkSync(filePath);
|
||||
} catch (error) {
|
||||
@ -29,40 +31,13 @@ test('writeFile', async () => {
|
||||
expect(fs.existsSync(filePath)).toBe(false);
|
||||
await writeFile({
|
||||
filePath,
|
||||
content: `/*
|
||||
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.
|
||||
*/
|
||||
|
||||
Test Write File`,
|
||||
content: `Test Write File`,
|
||||
});
|
||||
const res = await readFile(filePath);
|
||||
expect(res).toEqual(`/*
|
||||
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.
|
||||
*/
|
||||
|
||||
Test Write File`);
|
||||
expect(res).toEqual(`Test Write File`);
|
||||
try {
|
||||
fs.unlinkSync(filePath);
|
||||
} catch (error) {
|
||||
//pass
|
||||
}
|
||||
});
|
||||
|
@ -41,7 +41,6 @@ data:
|
||||
|
||||
test('page id missing', async () => {
|
||||
const app = {
|
||||
config: 'config',
|
||||
pages: [
|
||||
{
|
||||
type: 'PageHeaderMenu',
|
||||
@ -86,7 +85,6 @@ data:
|
||||
|
||||
test('page type missing', async () => {
|
||||
const app = {
|
||||
config: 'config',
|
||||
pages: [
|
||||
{
|
||||
id: 'page1',
|
||||
@ -121,7 +119,6 @@ data:
|
||||
|
||||
test('id incorrect type', async () => {
|
||||
const app = {
|
||||
config: 'config',
|
||||
pages: [
|
||||
{
|
||||
id: 1,
|
||||
@ -203,3 +200,25 @@ data:
|
||||
}
|
||||
--------------------------------`);
|
||||
});
|
||||
|
||||
// Not sure if this will ever happen, but testing for coverage
|
||||
test('dataPath not found in app', async () => {
|
||||
const app = {};
|
||||
const error = {
|
||||
keyword: 'type',
|
||||
dataPath: '.global',
|
||||
schemaPath: '#/properties/global/type',
|
||||
params: {
|
||||
type: 'object',
|
||||
},
|
||||
message: 'should be object',
|
||||
data: 'global',
|
||||
};
|
||||
const res = formatErrorMessage(error, app);
|
||||
expect(res).toEqual(`--------- Schema Error ---------
|
||||
message: should be object
|
||||
path: global
|
||||
data:
|
||||
|
||||
--------------------------------`);
|
||||
});
|
||||
|
@ -18,24 +18,23 @@ import path from 'path';
|
||||
import createComponentLoader from './componentLoader';
|
||||
import { ConfigurationError } from '../context/errors';
|
||||
|
||||
const CONFIGURATION_BASE_PATH = path.resolve(process.cwd(), 'src/test/config');
|
||||
|
||||
test('load component', async () => {
|
||||
const CONFIGURATION_BASE_PATH = path.resolve(process.cwd(), 'src/test/config');
|
||||
const pageLoader = createComponentLoader({ CONFIGURATION_BASE_PATH });
|
||||
const res = await pageLoader.load('global');
|
||||
const componentLoader = createComponentLoader({ CONFIGURATION_BASE_PATH });
|
||||
const res = await componentLoader.load('global');
|
||||
expect(res).toEqual({
|
||||
global: 'value',
|
||||
});
|
||||
});
|
||||
|
||||
test('load page, page does not exist', async () => {
|
||||
const CONFIGURATION_BASE_PATH = path.resolve(process.cwd(), 'src/test/config');
|
||||
const pageLoader = createComponentLoader({ CONFIGURATION_BASE_PATH });
|
||||
const res = await pageLoader.load('doesNotExist');
|
||||
test('load component, component does not exist', async () => {
|
||||
const componentLoader = createComponentLoader({ CONFIGURATION_BASE_PATH });
|
||||
const res = await componentLoader.load('doesNotExist');
|
||||
expect(res).toEqual(null);
|
||||
});
|
||||
|
||||
test('load component, invalid JSON', async () => {
|
||||
const CONFIGURATION_BASE_PATH = path.resolve(process.cwd(), 'src/test/config/pages');
|
||||
const pageLoader = createComponentLoader({ CONFIGURATION_BASE_PATH });
|
||||
await expect(pageLoader.load('invalid')).rejects.toThrow(ConfigurationError);
|
||||
const componentLoader = createComponentLoader({ CONFIGURATION_BASE_PATH });
|
||||
await expect(componentLoader.load('invalid')).rejects.toThrow(ConfigurationError);
|
||||
});
|
||||
|
@ -18,8 +18,9 @@ import path from 'path';
|
||||
import createPageLoader from './pageLoader';
|
||||
import { ConfigurationError } from '../context/errors';
|
||||
|
||||
const CONFIGURATION_BASE_PATH = path.resolve(process.cwd(), 'src/test/config');
|
||||
|
||||
test('load page', async () => {
|
||||
const CONFIGURATION_BASE_PATH = path.resolve(process.cwd(), 'src/test/config');
|
||||
const pageLoader = createPageLoader({ CONFIGURATION_BASE_PATH });
|
||||
const res = await pageLoader.load('page1');
|
||||
expect(res).toEqual({
|
||||
@ -29,14 +30,12 @@ test('load page', async () => {
|
||||
});
|
||||
|
||||
test('load page, page does not exist', async () => {
|
||||
const CONFIGURATION_BASE_PATH = path.resolve(process.cwd(), 'src/test/config');
|
||||
const pageLoader = createPageLoader({ CONFIGURATION_BASE_PATH });
|
||||
const res = await pageLoader.load('doesNotExist');
|
||||
expect(res).toEqual(null);
|
||||
});
|
||||
|
||||
test('load page, invalid JSON', async () => {
|
||||
const CONFIGURATION_BASE_PATH = path.resolve(process.cwd(), 'src/test/config');
|
||||
const pageLoader = createPageLoader({ CONFIGURATION_BASE_PATH });
|
||||
await expect(pageLoader.load('invalid')).rejects.toThrow(ConfigurationError);
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user