Skip to content

Commit

Permalink
Merge pull request #147 from kintone-labs/SSR-3283_AddUnitTest_review
Browse files Browse the repository at this point in the history
SSR-3283: review AddUnitTest
  • Loading branch information
hoangtran2506 committed Aug 21, 2023
2 parents 73c2bae + dbcd752 commit cc0cbed
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 112 deletions.
File renamed without changes.
70 changes: 4 additions & 66 deletions src/commands/Dev/test/appName.test.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
import { describe, expect, jest, test } from '@jest/globals';
import { program, Command } from 'commander';
import { readFileSync, writeFileSync } from 'jsonfile';
import { describe, expect, test } from '@jest/globals';
import { program } from 'commander';
import { writeFileSync } from 'jsonfile';

import devCommand, { devCommandHandle } from '../devCommand';
import devCommand from '../devCommand';
import {
APP_NAME,
DIR_BUILD_PATH,
WEBPACK_CONTENT
} from '../../../../unit_test/constant';
import { WRITE_FILE_OPTIONS } from '../../../constant';

import {
authCommandImplement,
createTemplate,
getRandomProjectName,
initProject
} from '../../../../unit_test/helper';
import {
dataInitDevCommand,
dataTest,
devCommandInit
} from './uploadConfig.test';

const initializeTestProject = async () => {
const projectName = getRandomProjectName();
Expand Down Expand Up @@ -57,60 +50,5 @@ describe('Dev command', () => {

expect(mainProgram.opts().appName).toEqual('');
});

let mainProgram: Command;
const readLineAsyncParam = jest.fn();

test('Should be "" when setting the value watch to false', async () => {
const currentDir = await devCommandInit('Plugin');
const config = readFileSync(`${currentDir}/config.json`);
config.uploadConfig.desktop.css = dataTest();
config.uploadConfig.mobile.js = dataTest();
Object.assign(config.uploadConfig, {
config: { css: dataTest(), js: dataTest() }
});

writeFileSync(`${currentDir}/config.json`, config, WRITE_FILE_OPTIONS);
const { webpackDevServer, commandConfig, responseMessage } =
dataInitDevCommand({ process, watch: false });

try {
await devCommandHandle({
ws: webpackDevServer,
cmd: commandConfig,
data: responseMessage,
readLineAsyncParam
});
expect(mainProgram.opts().appName).toEqual('');
} catch (error) {
expect(error).toEqual(error);
}
});

test('Should be "" setting the value watch to true', async () => {
const currentDir = await devCommandInit('Plugin');
const config = readFileSync(`${currentDir}/config.json`);
config.uploadConfig.desktop.css = dataTest();
config.uploadConfig.mobile.js = dataTest();
Object.assign(config.uploadConfig, {
config: { css: dataTest(), js: dataTest() }
});

writeFileSync(`${currentDir}/config.json`, config, WRITE_FILE_OPTIONS);
const { webpackDevServer, commandConfig, responseMessage } =
dataInitDevCommand({ process, watch: true });

try {
await devCommandHandle({
ws: webpackDevServer,
cmd: commandConfig,
data: responseMessage,
readLineAsyncParam
});
expect(mainProgram.opts().appName).toEqual('');
} catch (error) {
expect(error).toEqual(error);
}
});
});
});
File renamed without changes.
7 changes: 1 addition & 6 deletions src/commands/Dev/test/uploadConfig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,8 @@ import spawn from 'cross-spawn';
import { readFileSync, writeFileSync } from 'jsonfile';

import devCommand, { devCommandHandle } from '../devCommand';
import buildCommand from '../../Build/buildCommand';
import { WRITE_FILE_OPTIONS } from '../../../constant';
import {
APP_NAME,
DIR_BUILD_PATH,
WEBPACK_CONTENT
} from '../../../../unit_test/constant';
import { APP_NAME, DIR_BUILD_PATH } from '../../../../unit_test/constant';
import {
authCommandImplement,
createTemplateWithType,
Expand Down
43 changes: 43 additions & 0 deletions src/commands/Dev/test/watch.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { describe, expect, jest, test } from '@jest/globals';
import { Command } from 'commander';
import { readFileSync, writeFileSync } from 'jsonfile';
import { devCommandHandle } from '../devCommand';
import { WRITE_FILE_OPTIONS } from '../../../constant';
import {
dataInitDevCommand,
dataTest,
devCommandInit
} from './uploadConfig.test';

describe('Dev command', () => {
describe('Watch', () => {
let mainProgram: Command;
const readLineAsyncParam = jest.fn();

test('Should be "" when setting the value watch to false', async () => {
const currentDir = await devCommandInit('Plugin');
const config = readFileSync(`${currentDir}/config.json`);
config.uploadConfig.desktop.css = dataTest();
config.uploadConfig.mobile.js = dataTest();
Object.assign(config.uploadConfig, {
config: { css: dataTest(), js: dataTest() }
});

writeFileSync(`${currentDir}/config.json`, config, WRITE_FILE_OPTIONS);
const { webpackDevServer, commandConfig, responseMessage } =
dataInitDevCommand({ process, watch: false });

try {
await devCommandHandle({
ws: webpackDevServer,
cmd: commandConfig,
data: responseMessage,
readLineAsyncParam
});
expect(mainProgram.opts().appName).toEqual('');
} catch (error) {
expect(error).toEqual(error);
}
});
});
});
57 changes: 57 additions & 0 deletions src/commands/Initialize/test/appFolder.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { describe, expect, test } from '@jest/globals';
import { generateAppFolder } from '../generator';
import { AppOption } from '../../../dto/app';
import {
createTemplateWithArgv,
getRandomProjectName,
initProject
} from '../../../../unit_test/helper';
import { APP_NAME, DIR_BUILD_PATH } from '../../../../unit_test/constant';

const initializeTestProject = async (initProjectType: any = initProject) => {
const projectName = getRandomProjectName();
const currentDir = `${DIR_BUILD_PATH}/${projectName}`;
await initProjectType(DIR_BUILD_PATH, projectName);
const argv = [
'node',
'dist',
'create-template',
'--quick',
'--app-name',
APP_NAME,
'--app-id',
'100'
];
await createTemplateWithArgv(projectName, argv);

return { projectName, currentDir };
};

describe('Initialize command', () => {
const appOption = {
appName: APP_NAME,
type: 'Customization',
appID: '100',
scope: 'ALL'
} as unknown as AppOption;

describe('App Folder', () => {
test('Should create a folder when the app option is set correctly', async () => {
await initializeTestProject();
appOption.setAuth = true;
appOption.appName = 'test-app-1';
appOption.useCybozuLint = true;
const creationSuccessful = generateAppFolder(appOption);

expect(creationSuccessful).toBe(false);
});

test('Should display "App folder existed." when the app name already exists', async () => {
appOption.useCybozuLint = true;
appOption.appName = 'test-app-1';
const errorMessage = generateAppFolder(appOption);

expect(errorMessage).toBe('App folder existed.');
});
});
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { describe, expect, test } from '@jest/globals';
import validator from '../validator';
import {
authCommandImplement,
createTemplateWithArgv,
Expand Down Expand Up @@ -33,24 +32,6 @@ const initializeTestProject = async (

describe('Initialize command', () => {
describe('Validator App Type', () => {
test('Should be "Invalid App Type" when setting quick option', async () => {
const argv = [
'node',
'dist',
'create-template',
'--quick',
'--app-name',
'test-app',
'--app-id',
'100'
];
await initializeTestProject(argv);
const params = { type: 'invalid type' };
const errorMessage = validator.appValidator(params) as string;

expect(errorMessage).toEqual('Invalid App Type');
});

test('Should use React template when the preset is set to React', async () => {
const argv = [
'node',
Expand Down
22 changes: 1 addition & 21 deletions src/commands/Initialize/test/cybozuLint.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, test } from '@jest/globals';
import { configureCybozuLint, generateAppFolder } from '../generator';
import { configureCybozuLint } from '../generator';
import { AppOption } from '../../../dto/app';
import {
createTemplateWithArgv,
Expand Down Expand Up @@ -93,24 +93,4 @@ describe('Initialize command', () => {
expect(isCybozuLintInstalled).toBe(true);
});
});

describe('App Folder', () => {
test('Should create a folder when the app option is set correctly', async () => {
await initializeTestProject();
appOption.setAuth = true;
appOption.appName = 'test-app-1';
appOption.useCybozuLint = true;
const creationSuccessful = generateAppFolder(appOption);

expect(creationSuccessful).toBe(false);
});

test('Should display "App folder existed." when the app name already exists', async () => {
appOption.useCybozuLint = true;
appOption.appName = 'test-app-1';
const errorMessage = generateAppFolder(appOption);

expect(errorMessage).toBe('App folder existed.');
});
});
});

0 comments on commit cc0cbed

Please sign in to comment.