Skip to content

Commit

Permalink
test: mock out git
Browse files Browse the repository at this point in the history
  • Loading branch information
kellyjosephprice committed Feb 7, 2024
1 parent 58bcc48 commit 9051dda
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
29 changes: 29 additions & 0 deletions __tests__/cmds/docs/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import DocsCommand from '../../../src/cmds/docs/index.js';
import GuidesCommand from '../../../src/cmds/guides/index.js';
import APIError from '../../../src/lib/apiError.js';
import configstore from '../../../src/lib/configstore.js';
import { git } from '../../../src/lib/createGHA/index.js';
import getAPIMock, { getAPIMockWithVersionHeader } from '../../helpers/get-api-mock.js';
import { after, before } from '../../helpers/get-gha-setup.js';
import hashFileContents from '../../helpers/hash-file-contents.js';
Expand Down Expand Up @@ -105,6 +106,16 @@ describe('rdme docs', () => {
});

describe('existing docs', () => {
beforeEach(() => {
git.checkIsRepo = vi.fn(() => {
return Promise.resolve(true) as unknown as Response<boolean>;
});

git.remote = vi.fn(() => {
return Promise.resolve('origin') as unknown as Response<string>;
});
});

let simpleDoc;
let anotherDoc;

Expand Down Expand Up @@ -275,6 +286,15 @@ describe('rdme docs', () => {
});

describe('new docs', () => {
beforeEach(() => {
git.checkIsRepo = vi.fn(() => {
return Promise.resolve(true) as unknown as Response<boolean>;
});

git.remote = vi.fn(() => {
return Promise.resolve('origin') as unknown as Response<string>;
});
});
it('should create new doc', async () => {
const slug = 'new-doc';
const id = '1234';
Expand Down Expand Up @@ -392,6 +412,15 @@ describe('rdme docs', () => {
});

describe('slug metadata', () => {
beforeEach(() => {
git.checkIsRepo = vi.fn(() => {
return Promise.resolve(true) as unknown as Response<boolean>;
});

git.remote = vi.fn(() => {
return Promise.resolve('origin') as unknown as Response<string>;
});
});
it('should use provided slug', async () => {
const slug = 'new-doc-slug';
const id = '1234';
Expand Down
19 changes: 16 additions & 3 deletions __tests__/cmds/docs/single.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { Response } from 'simple-git';

import fs from 'node:fs';
import path from 'node:path';

Expand All @@ -9,6 +11,7 @@ import { describe, beforeAll, afterAll, beforeEach, afterEach, it, expect, vi }

import DocsCommand from '../../../src/cmds/docs/index.js';
import APIError from '../../../src/lib/apiError.js';
import { git } from '../../../src/lib/createGHA/index.js';
import getAPIMock, { getAPIMockWithVersionHeader } from '../../helpers/get-api-mock.js';
import hashFileContents from '../../helpers/hash-file-contents.js';
import { after as afterGHAEnv, before as beforeGHAEnv } from '../../helpers/setup-gha-env.js';
Expand All @@ -27,6 +30,16 @@ describe('rdme docs (single)', () => {
nock.disableNetConnect();
});

beforeEach(() => {
git.checkIsRepo = vi.fn(() => {
return Promise.resolve(true) as unknown as Response<boolean>;
});

git.remote = vi.fn(() => {
return Promise.resolve('origin') as unknown as Response<string>;
});
});

afterAll(() => nock.cleanAll());

it('should prompt for login if no API key provided', async () => {
Expand Down Expand Up @@ -92,9 +105,9 @@ describe('rdme docs (single)', () => {
.basicAuth({ user: key })
.reply(200, { version });

await expect(
docs.run({ filePath: `./__tests__/${fixturesBaseDir}/new-docs/new-doc.md`, key, version }),
).resolves.toBe(
const promise = docs.run({ filePath: `./__tests__/${fixturesBaseDir}/new-docs/new-doc.md`, key, version });

await expect(promise).resolves.toBe(
`🌱 successfully created 'new-doc' (ID: 1234) with contents from ./__tests__/${fixturesBaseDir}/new-docs/new-doc.md`,
);

Expand Down

0 comments on commit 9051dda

Please sign in to comment.