Skip to content

Commit

Permalink
test getCodePath
Browse files Browse the repository at this point in the history
  • Loading branch information
jperl committed Mar 6, 2020
1 parent 1e05a78 commit 4a7dd3c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/create-code/CreateManager.ts
Expand Up @@ -87,7 +87,7 @@ export class CreateManager {
this._collector.on('elementevent', event => this.update(event));
}

protected async update(event: ElementEvent) {
protected async update(event: ElementEvent): Promise<void> {
this._events.push(event);

const steps = buildSteps({
Expand All @@ -101,7 +101,7 @@ export class CreateManager {
]);
}

protected async finalize() {
protected async finalize(): Promise<void> {
const shouldSave = await promptSaveRepl(this._codeUpdater.path());
if (shouldSave) {
await this._codeUpdater.finalize();
Expand Down
8 changes: 7 additions & 1 deletion src/create-code/create.ts
@@ -1,4 +1,5 @@
import callsites from 'callsites';
import Debug from 'debug';
import { pathExists, readFile } from 'fs-extra';
import { findLast } from 'lodash';
import { basename, dirname, join } from 'path';
Expand All @@ -13,15 +14,20 @@ type CreateOptions = {
selectorPath?: string;
};

const debug = Debug('qawolf:create');

export const getCodePath = async (): Promise<string> => {
const callerFileNames = callsites().map(c => c.getFileName());
debug(`search caller files for ${CREATE_HANDLE} %j`, callerFileNames);

const codes = await Promise.all(
callerFileNames.map(async filename => {
let code = '';

if (await pathExists(filename)) {
code = await readFile(filename, 'utf8');
}

return { code, filename };
}),
);
Expand All @@ -38,7 +44,7 @@ export const getCodePath = async (): Promise<string> => {
return item.filename;
};

export const getSelectorPath = (codePath: string) => {
export const getSelectorPath = (codePath: string): string => {
const codeName = basename(codePath)
.split('.')
.slice(0, -1);
Expand Down
9 changes: 9 additions & 0 deletions test/create-code/create.test.ts
@@ -0,0 +1,9 @@
import { resolve } from 'path';
import { getCodePath } from '../../src/create-code/create';

describe('getCodePath', () => {
it('finds the caller file with "await qawolf.create"', async () => {
const path = await getCodePath();
expect(resolve(path)).toEqual(resolve(__filename));
});
});

0 comments on commit 4a7dd3c

Please sign in to comment.