Skip to content

Commit

Permalink
Merge pull request #236 from jaredwray/adding-in-mocks-for-axios
Browse files Browse the repository at this point in the history
adding in mocks for axios
  • Loading branch information
jaredwray committed Jan 10, 2024
2 parents b999131 + 2967cfc commit 5525220
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 26 deletions.
15 changes: 11 additions & 4 deletions src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,13 @@ export class Github {
const url = `${this.options.api}/repos/${this.options.author}/${this.options.repo}/releases`;
try {
const result = await axios.get(url);
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return result.data;

if (result && result.data.length > 0) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return result.data;
}

return {};
} catch (error: unknown) {
const typedError = error as {response: {status: number}};
if (typedError.response?.status === 404) {
Expand All @@ -55,8 +60,10 @@ export class Github {
const url = `${this.options.api}/repos/${this.options.author}/${this.options.repo}/contributors`;
try {
const result = await axios.get(url);
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return result.data;
if (result && result.data.length > 0) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return result.data;
}
} catch (error: unknown) {
const typedError = error as {response: {status: number}};
if (typedError.response?.status === 404) {
Expand Down
11 changes: 5 additions & 6 deletions src/writr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {register} from 'ts-node';
import packageJson from '../package.json';
import {WritrOptions} from './options.js';
import {WritrConsole} from './console.js';
import {WritrBuilder} from './builder.js';

export default class Writr {
private _options: WritrOptions = new WritrOptions();
Expand Down Expand Up @@ -62,6 +63,8 @@ export default class Writr {
this.options.outputPath = consoleProcess.args.output;
}

console.log(consoleProcess);

switch (consoleProcess.command) {
case 'init': {
const isTypescript = fs.existsSync('./tsconfig.json') ?? false;
Expand All @@ -79,18 +82,14 @@ export default class Writr {
break;
}

case 'build': {
this._console.log('Build');
break;
}

case 'serve': {
this._console.log('Serve');
break;
}

default: {
this._console.log('Build');
const builder = new WritrBuilder();
await builder.build();
break;
}
}
Expand Down
26 changes: 25 additions & 1 deletion test/builder.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import {expect, it, describe, vi} from 'vitest';
import {afterEach, beforeEach, expect, it, describe, vi} from 'vitest';
import * as fs from 'fs-extra';
import axios from 'axios';
import {WritrBuilder, type WritrData} from '../src/builder.js';
import {WritrOptions} from '../src/options.js';
import githubMockContributors from './fixtures/data-mocks/github-contributors.json';
import githubMockReleases from './fixtures/data-mocks/github-releases.json';

vi.mock('axios');

describe('WritrBuilder', () => {
const writrData: WritrData = {
Expand All @@ -14,6 +18,26 @@ describe('WritrBuilder', () => {
outputPath: 'test/temp-sitemap-test',
};

afterEach(() => {
// Reset the mock after each test
vi.resetAllMocks();
});
beforeEach(() => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
(axios.get as any).mockImplementation(async (url: string) => {
if (url.endsWith('releases')) {
return {data: githubMockReleases};
}

if (url.endsWith('contributors')) {
return {data: githubMockContributors};
}

// Default response or throw an error if you prefer
return {data: {}};
});
});

it('should initiate', () => {
const builder = new WritrBuilder();
expect(builder).toBeTruthy();
Expand Down
17 changes: 16 additions & 1 deletion test/github.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {afterEach, describe, expect, it, vi} from 'vitest';
import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest';
import axios from 'axios';
import {Github, type GithubOptions} from '../src/github.js';
import githubMockContributors from './fixtures/data-mocks/github-contributors.json';
Expand All @@ -17,6 +17,21 @@ describe('Github', () => {
// Reset the mock after each test
vi.resetAllMocks();
});
beforeEach(() => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
(axios.get as any).mockImplementation(async (url: string) => {
if (url.endsWith('releases')) {
return {data: githubMockReleases};
}

if (url.endsWith('contributors')) {
return {data: githubMockContributors};
}

// Default response or throw an error if you prefer
return {data: {}};
});
});

it('should be able to initialize', () => {
const github = new Github(defaultOptions);
Expand Down
44 changes: 30 additions & 14 deletions test/writr.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import process from 'node:process';
import {expect, it, describe} from 'vitest';
import {afterEach, beforeEach, expect, it, describe, vi} from 'vitest';
import fs from 'fs-extra';
import axios from 'axios';
import Writr, {WritrHelpers} from '../src/writr.js';
import {WritrOptions} from '../src/options.js';
import githubMockContributors from './fixtures/data-mocks/github-contributors.json';
import githubMockReleases from './fixtures/data-mocks/github-releases.json';

const defaultOptions: WritrOptions = new WritrOptions({
templatePath: './custom-template',
Expand All @@ -14,7 +17,29 @@ const defaultOptions: WritrOptions = new WritrOptions({
siteUrl: 'https://custom-url.com',
});

vi.mock('axios');

describe('writr', () => {
afterEach(() => {
// Reset the mock after each test
vi.resetAllMocks();
});
beforeEach(() => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
(axios.get as any).mockImplementation(async (url: string) => {
if (url.endsWith('releases')) {
return {data: githubMockReleases};
}

if (url.endsWith('contributors')) {
return {data: githubMockContributors};
}

// Default response or throw an error if you prefer
return {data: {}};
});
});

it('should be able to initialize', () => {
const writr = new Writr();
expect(writr).toBeDefined();
Expand Down Expand Up @@ -50,19 +75,10 @@ describe('writr', () => {
});
it('if no parameters then it should build', async () => {
const writr = new Writr(defaultOptions);
const consoleLog = console.log;
let consoleMessage = '';

console.log = message => {
if (typeof message === 'string' && message.includes('Build')) {
consoleMessage = message;
}
};

const outputPath = './test/noparam-custom-site';
process.argv = ['node', 'writr', '-o', outputPath];
await writr.execute(process);

expect(consoleMessage).toContain('Build');
console.log = consoleLog;
});
it('is a single page site or not', () => {
const writr = new Writr(defaultOptions);
Expand Down Expand Up @@ -171,10 +187,10 @@ describe('writr execute', () => {
});
it('should build based on the build command', async () => {
const writr = new Writr(defaultOptions);
const sitePath = './custom-site/dist';
const outputPath = './custom-site/dist';
const consoleLog = console.log;
let consoleMessage = '';
process.argv = ['node', 'writr', 'build', '-o', sitePath];
process.argv = ['node', 'writr', 'build', '-o', outputPath];
console.log = message => {
if (typeof message === 'string' && message.includes('Build')) {
consoleMessage = message;
Expand Down

0 comments on commit 5525220

Please sign in to comment.