Skip to content

Commit

Permalink
tests: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Jan 11, 2021
1 parent 6a585ce commit bba1a7c
Showing 1 changed file with 53 additions and 11 deletions.
64 changes: 53 additions & 11 deletions packages/generators/__tests__/addon-generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,14 @@ describe('addon generator', () => {
// we call this unwanted path doubleGenPath
const doubleGenPath = path.join(genPath, genName);

beforeAll(() => {
rimraf.sync(testAssetsPath);
fs.mkdirSync(genPath, { recursive: true });
// set the working directory to here so that the addon directory is
// generated in ./test-assets/test-addon
process.chdir(genPath);
packageMock = getPackageManager as jest.Mock;
});

afterAll(() => {
rimraf.sync(testAssetsPath);
});

beforeEach(() => {
// eslint-disable-next-line @typescript-eslint/no-empty-function
const Gen = addonGenerator([], '', [], [], () => {});

gen = new Gen(null, null);
gen.props = {
name: genName,
Expand All @@ -43,9 +35,20 @@ describe('addon generator', () => {
});

it('schedules install using npm', () => {
const defaultCwd = process.cwd();

rimraf.sync(testAssetsPath);
fs.mkdirSync(genPath, { recursive: true });

// set the working directory to here so that the addon directory is
// generated in ./test-assets/test-addon
process.chdir(genPath);

packageMock = getPackageManager as jest.Mock;
packageMock.mockReturnValue('npm');

gen.install();

expect(installMock.mock.calls.length).toEqual(1);
expect(installMock.mock.calls[0]).toEqual([
'npm',
Expand All @@ -54,12 +57,24 @@ describe('addon generator', () => {
'save-dev': true,
},
]);

process.chdir(defaultCwd);
});

it('schedules install using yarn', () => {
const defaultCwd = process.cwd();

rimraf.sync(testAssetsPath);
fs.mkdirSync(genPath, { recursive: true });
// set the working directory to here so that the addon directory is
// generated in ./test-assets/test-addon
process.chdir(genPath);

packageMock = getPackageManager as jest.Mock;
packageMock.mockReturnValue('yarn');

gen.install();

expect(installMock.mock.calls.length).toEqual(1);
expect(installMock.mock.calls[0]).toEqual([
'yarn',
Expand All @@ -68,11 +83,26 @@ describe('addon generator', () => {
dev: true,
},
]);

process.chdir(defaultCwd);
});

it('does not create new directory when current directory matches addon name', () => {
const defaultCwd = process.cwd();

rimraf.sync(testAssetsPath);
fs.mkdirSync(genPath, { recursive: true });

// set the working directory to here so that the addon directory is
// generated in ./test-assets/test-addon
process.chdir(genPath);

packageMock = getPackageManager as jest.Mock;

expect(fs.existsSync(genPath)).toBeTruthy();

gen.default();

expect(fs.existsSync(genPath)).toBeTruthy();
expect(fs.existsSync(doubleGenPath)).toBeFalsy();

Expand All @@ -81,14 +111,26 @@ describe('addon generator', () => {
// generator above
// this is switching the working directory as follows:
// ./test-assets/test-addon -> ./test-assets
process.chdir(testAssetsPath);
rimraf.sync(genPath);

process.chdir(defaultCwd);
});

it('creates a new directory for the generated addon', () => {
expect(fs.existsSync(genPath)).toBeFalsy();
const defaultCwd = process.cwd();

rimraf.sync(testAssetsPath);
fs.mkdirSync(genPath, { recursive: true });

// set the working directory to here so that the addon directory is
// generated in ./test-assets/test-addon
process.chdir(genPath);

gen.default();

expect(fs.existsSync(genPath)).toBeTruthy();
expect(fs.existsSync(doubleGenPath)).toBeFalsy();

process.chdir(defaultCwd);
});
});

0 comments on commit bba1a7c

Please sign in to comment.