Skip to content

Commit

Permalink
chore: rename test mocks, smol comment/type fixes (#1076)
Browse files Browse the repository at this point in the history
  • Loading branch information
kanadgupta authored Nov 19, 2024
1 parent f41dd56 commit 932a6d4
Show file tree
Hide file tree
Showing 22 changed files with 365 additions and 298 deletions.
41 changes: 28 additions & 13 deletions __tests__/commands/categories/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import nock from 'nock';
import { describe, beforeAll, afterEach, it, expect } from 'vitest';

import Command from '../../../src/commands/categories/create.js';
import getAPIMock, { getAPIMockWithVersionHeader } from '../../helpers/get-api-mock.js';
import { getAPIV1Mock, getAPIV1MockWithVersionHeader } from '../../helpers/get-api-mock.js';
import { runCommand } from '../../helpers/setup-oclif-config.js';

const key = 'API_KEY';
Expand Down Expand Up @@ -33,20 +33,23 @@ describe('rdme categories:create', () => {
});

it('should create a new category if the title and type do not match and preventDuplicates=true', async () => {
const getMock = getAPIMockWithVersionHeader(version)
const getMock = getAPIV1MockWithVersionHeader(version)
.persist()
.get('/api/v1/categories?perPage=20&page=1')
.basicAuth({ user: key })
.reply(200, [{ title: 'Existing Category', slug: 'existing-category', type: 'guide' }], {
'x-total-count': '1',
});

const postMock = getAPIMockWithVersionHeader(version)
const postMock = getAPIV1MockWithVersionHeader(version)
.post('/api/v1/categories')
.basicAuth({ user: key })
.reply(201, { title: 'New Category', slug: 'new-category', type: 'guide', id: '123' });

const versionMock = getAPIMock().get(`/api/v1/version/${version}`).basicAuth({ user: key }).reply(200, { version });
const versionMock = getAPIV1Mock()
.get(`/api/v1/version/${version}`)
.basicAuth({ user: key })
.reply(200, { version });

await expect(
run(['New Category', '--categoryType', 'guide', '--key', key, '--version', '1.0.0', '--preventDuplicates']),
Expand All @@ -58,20 +61,23 @@ describe('rdme categories:create', () => {
});

it('should create a new category if the title matches but the type does not match and preventDuplicates=true', async () => {
const getMock = getAPIMockWithVersionHeader(version)
const getMock = getAPIV1MockWithVersionHeader(version)
.persist()
.get('/api/v1/categories?perPage=20&page=1')
.basicAuth({ user: key })
.reply(200, [{ title: 'Category', slug: 'category', type: 'guide' }], {
'x-total-count': '1',
});

const postMock = getAPIMockWithVersionHeader(version)
const postMock = getAPIV1MockWithVersionHeader(version)
.post('/api/v1/categories')
.basicAuth({ user: key })
.reply(201, { title: 'Category', slug: 'category', type: 'reference', id: '123' });

const versionMock = getAPIMock().get(`/api/v1/version/${version}`).basicAuth({ user: key }).reply(200, { version });
const versionMock = getAPIV1Mock()
.get(`/api/v1/version/${version}`)
.basicAuth({ user: key })
.reply(200, { version });

await expect(
run(['--categoryType', 'reference', '--key', key, '--version', '1.0.0', '--preventDuplicates', 'Category']),
Expand All @@ -83,12 +89,15 @@ describe('rdme categories:create', () => {
});

it('should create a new category if the title and type match and preventDuplicates=false', async () => {
const postMock = getAPIMockWithVersionHeader(version)
const postMock = getAPIV1MockWithVersionHeader(version)
.post('/api/v1/categories')
.basicAuth({ user: key })
.reply(201, { title: 'Category', slug: 'category', type: 'reference', id: '123' });

const versionMock = getAPIMock().get(`/api/v1/version/${version}`).basicAuth({ user: key }).reply(200, { version });
const versionMock = getAPIV1Mock()
.get(`/api/v1/version/${version}`)
.basicAuth({ user: key })
.reply(200, { version });

await expect(run(['Category', '--categoryType', 'guide', '--key', key, '--version', '1.0.0'])).resolves.toBe(
"🌱 successfully created 'Category' with a type of 'reference' and an id of '123'",
Expand All @@ -99,15 +108,18 @@ describe('rdme categories:create', () => {
});

it('should not create a new category if the title and type match and preventDuplicates=true', async () => {
const getMock = getAPIMockWithVersionHeader(version)
const getMock = getAPIV1MockWithVersionHeader(version)
.persist()
.get('/api/v1/categories?perPage=20&page=1')
.basicAuth({ user: key })
.reply(200, [{ title: 'Category', slug: 'category', type: 'guide', id: '123' }], {
'x-total-count': '1',
});

const versionMock = getAPIMock().get(`/api/v1/version/${version}`).basicAuth({ user: key }).reply(200, { version });
const versionMock = getAPIV1Mock()
.get(`/api/v1/version/${version}`)
.basicAuth({ user: key })
.reply(200, { version });

await expect(
run(['Category', '--categoryType', 'guide', '--key', key, '--version', '1.0.0', '--preventDuplicates']),
Expand All @@ -122,15 +134,18 @@ describe('rdme categories:create', () => {
});

it('should not create a new category if the non case sensitive title and type match and preventDuplicates=true', async () => {
const getMock = getAPIMockWithVersionHeader(version)
const getMock = getAPIV1MockWithVersionHeader(version)
.persist()
.get('/api/v1/categories?perPage=20&page=1')
.basicAuth({ user: key })
.reply(200, [{ title: 'Category', slug: 'category', type: 'guide', id: '123' }], {
'x-total-count': '1',
});

const versionMock = getAPIMock().get(`/api/v1/version/${version}`).basicAuth({ user: key }).reply(200, { version });
const versionMock = getAPIV1Mock()
.get(`/api/v1/version/${version}`)
.basicAuth({ user: key })
.reply(200, { version });

await expect(
run(['Category', '--categoryType', 'guide', '--key', key, '--version', '1.0.0', '--preventDuplicates']),
Expand Down
16 changes: 11 additions & 5 deletions __tests__/commands/categories/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import nock from 'nock';
import { describe, beforeAll, afterEach, it, expect } from 'vitest';

import Command from '../../../src/commands/categories/index.js';
import getAPIMock, { getAPIMockWithVersionHeader } from '../../helpers/get-api-mock.js';
import { getAPIV1Mock, getAPIV1MockWithVersionHeader } from '../../helpers/get-api-mock.js';
import { runCommand } from '../../helpers/setup-oclif-config.js';

const key = 'API_KEY';
Expand All @@ -19,15 +19,18 @@ describe('rdme categories', () => {
afterEach(() => nock.cleanAll());

it('should return all categories for a single page', async () => {
const getMock = getAPIMockWithVersionHeader(version)
const getMock = getAPIV1MockWithVersionHeader(version)
.persist()
.get('/api/v1/categories?perPage=20&page=1')
.basicAuth({ user: key })
.reply(200, [{ title: 'One Category', slug: 'one-category', type: 'guide' }], {
'x-total-count': '1',
});

const versionMock = getAPIMock().get(`/api/v1/version/${version}`).basicAuth({ user: key }).reply(200, { version });
const versionMock = getAPIV1Mock()
.get(`/api/v1/version/${version}`)
.basicAuth({ user: key })
.reply(200, { version });

await expect(run(['--key', key, '--version', '1.0.0'])).resolves.toBe(
JSON.stringify([{ title: 'One Category', slug: 'one-category', type: 'guide' }], null, 2),
Expand All @@ -38,7 +41,7 @@ describe('rdme categories', () => {
});

it('should return all categories for multiple pages', async () => {
const getMock = getAPIMockWithVersionHeader(version)
const getMock = getAPIV1MockWithVersionHeader(version)
.persist()
.get('/api/v1/categories?perPage=20&page=1')
.basicAuth({ user: key })
Expand All @@ -51,7 +54,10 @@ describe('rdme categories', () => {
'x-total-count': '21',
});

const versionMock = getAPIMock().get(`/api/v1/version/${version}`).basicAuth({ user: key }).reply(200, { version });
const versionMock = getAPIV1Mock()
.get(`/api/v1/version/${version}`)
.basicAuth({ user: key })
.reply(200, { version });

await expect(run(['--key', key, '--version', '1.0.0'])).resolves.toBe(
JSON.stringify(
Expand Down
26 changes: 13 additions & 13 deletions __tests__/commands/changelogs/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { describe, beforeAll, afterAll, beforeEach, it, expect } from 'vitest';

import Command from '../../../src/commands/changelogs.js';
import APIError from '../../../src/lib/apiError.js';
import getAPIMock from '../../helpers/get-api-mock.js';
import { getAPIV1Mock } from '../../helpers/get-api-mock.js';
import hashFileContents from '../../helpers/hash-file-contents.js';
import { runCommand } from '../../helpers/setup-oclif-config.js';

Expand Down Expand Up @@ -67,15 +67,15 @@ describe('rdme changelogs', () => {
it('should fetch changelog and merge with what is returned', () => {
expect.assertions(1);

const getMocks = getAPIMock()
const getMocks = getAPIV1Mock()
.get('/api/v1/changelogs/simple-doc')
.basicAuth({ user: key })
.reply(200, { slug: simpleDoc.slug, lastUpdatedHash: 'anOldHash' })
.get('/api/v1/changelogs/another-doc')
.basicAuth({ user: key })
.reply(200, { slug: anotherDoc.slug, lastUpdatedHash: 'anOldHash' });

const updateMocks = getAPIMock()
const updateMocks = getAPIV1Mock()
.put('/api/v1/changelogs/simple-doc', {
body: simpleDoc.doc.content,
lastUpdatedHash: simpleDoc.hash,
Expand Down Expand Up @@ -112,7 +112,7 @@ describe('rdme changelogs', () => {
it('should return changelog update info for dry run', () => {
expect.assertions(1);

const getMocks = getAPIMock()
const getMocks = getAPIV1Mock()
.get('/api/v1/changelogs/simple-doc')
.basicAuth({ user: key })
.reply(200, { slug: simpleDoc.slug, lastUpdatedHash: 'anOldHash' })
Expand Down Expand Up @@ -141,7 +141,7 @@ describe('rdme changelogs', () => {
it('should not send requests for changelogs that have not changed', () => {
expect.assertions(1);

const getMocks = getAPIMock()
const getMocks = getAPIV1Mock()
.get('/api/v1/changelogs/simple-doc')
.basicAuth({ user: key })
.reply(200, { slug: simpleDoc.slug, lastUpdatedHash: simpleDoc.hash })
Expand All @@ -164,7 +164,7 @@ describe('rdme changelogs', () => {
it('should adjust "no changes" message if in dry run', () => {
expect.assertions(1);

const getMocks = getAPIMock()
const getMocks = getAPIV1Mock()
.get('/api/v1/changelogs/simple-doc')
.basicAuth({ user: key })
.reply(200, { slug: simpleDoc.slug, lastUpdatedHash: simpleDoc.hash })
Expand Down Expand Up @@ -192,7 +192,7 @@ describe('rdme changelogs', () => {
const doc = frontMatter(fs.readFileSync(path.join(fullFixturesDir, `/new-docs/${slug}.md`)));
const hash = hashFileContents(fs.readFileSync(path.join(fullFixturesDir, `/new-docs/${slug}.md`)));

const getMock = getAPIMock()
const getMock = getAPIV1Mock()
.get(`/api/v1/changelogs/${slug}`)
.basicAuth({ user: key })
.reply(404, {
Expand All @@ -202,7 +202,7 @@ describe('rdme changelogs', () => {
help: 'If you need help, email [email protected] and mention log "fake-metrics-uuid".',
});

const postMock = getAPIMock()
const postMock = getAPIV1Mock()
.post('/api/v1/changelogs', { slug, body: doc.content, ...doc.data, lastUpdatedHash: hash })
.basicAuth({ user: key })
.reply(201, { slug, _id: id, body: doc.content, ...doc.data, lastUpdatedHash: hash });
Expand All @@ -219,7 +219,7 @@ describe('rdme changelogs', () => {
const slug = 'new-doc';
const doc = frontMatter(fs.readFileSync(path.join(fullFixturesDir, `/new-docs/${slug}.md`)));

const getMock = getAPIMock()
const getMock = getAPIV1Mock()
.get(`/api/v1/changelogs/${slug}`)
.basicAuth({ user: key })
.reply(404, {
Expand Down Expand Up @@ -254,7 +254,7 @@ describe('rdme changelogs', () => {

const hash = hashFileContents(fs.readFileSync(path.join(fullFixturesDir, `/${folder}/${slug}.md`)));

const getMocks = getAPIMock()
const getMocks = getAPIV1Mock()
.get(`/api/v1/changelogs/${slug}`)
.basicAuth({ user: key })
.reply(404, {
Expand All @@ -264,7 +264,7 @@ describe('rdme changelogs', () => {
help: 'If you need help, email [email protected] and mention log "fake-metrics-uuid".',
});

const postMocks = getAPIMock()
const postMocks = getAPIV1Mock()
.post('/api/v1/changelogs', { slug, body: doc.content, ...doc.data, lastUpdatedHash: hash })
.basicAuth({ user: key })
.reply(400, errorObject);
Expand All @@ -290,7 +290,7 @@ describe('rdme changelogs', () => {
const doc = frontMatter(fs.readFileSync(path.join(fullFixturesDir, `/slug-docs/${slug}.md`)));
const hash = hashFileContents(fs.readFileSync(path.join(fullFixturesDir, `/slug-docs/${slug}.md`)));

const getMock = getAPIMock()
const getMock = getAPIV1Mock()
.get(`/api/v1/changelogs/${doc.data.slug}`)
.basicAuth({ user: key })
.reply(404, {
Expand All @@ -300,7 +300,7 @@ describe('rdme changelogs', () => {
help: 'If you need help, email [email protected] and mention log "fake-metrics-uuid".',
});

const postMock = getAPIMock()
const postMock = getAPIV1Mock()
.post('/api/v1/changelogs', { slug, body: doc.content, ...doc.data, lastUpdatedHash: hash })
.basicAuth({ user: key })
.reply(201, { slug: doc.data.slug, _id: id, body: doc.content, ...doc.data, lastUpdatedHash: hash });
Expand Down
24 changes: 12 additions & 12 deletions __tests__/commands/changelogs/single.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { describe, beforeAll, afterAll, beforeEach, it, expect } from 'vitest';

import Command from '../../../src/commands/changelogs.js';
import APIError from '../../../src/lib/apiError.js';
import getAPIMock from '../../helpers/get-api-mock.js';
import { getAPIV1Mock } from '../../helpers/get-api-mock.js';
import hashFileContents from '../../helpers/hash-file-contents.js';
import { runCommand } from '../../helpers/setup-oclif-config.js';

Expand Down Expand Up @@ -49,7 +49,7 @@ describe('rdme changelogs (single)', () => {
const doc = frontMatter(fs.readFileSync(path.join(fullFixturesDir, `/new-docs/${slug}.md`)));
const hash = hashFileContents(fs.readFileSync(path.join(fullFixturesDir, `/new-docs/${slug}.md`)));

const getMock = getAPIMock()
const getMock = getAPIV1Mock()
.get(`/api/v1/changelogs/${slug}`)
.basicAuth({ user: key })
.reply(404, {
Expand All @@ -59,7 +59,7 @@ describe('rdme changelogs (single)', () => {
help: 'If you need help, email [email protected] and mention log "fake-metrics-uuid".',
});

const postMock = getAPIMock()
const postMock = getAPIV1Mock()
.post('/api/v1/changelogs', { slug, body: doc.content, ...doc.data, lastUpdatedHash: hash })
.basicAuth({ user: key })
.reply(201, { slug, _id: id, body: doc.content, ...doc.data });
Expand All @@ -76,7 +76,7 @@ describe('rdme changelogs (single)', () => {
const slug = 'new-doc';
const doc = frontMatter(fs.readFileSync(path.join(fullFixturesDir, `/new-docs/${slug}.md`)));

const getMock = getAPIMock()
const getMock = getAPIV1Mock()
.get(`/api/v1/changelogs/${slug}`)
.basicAuth({ user: key })
.reply(404, {
Expand Down Expand Up @@ -113,7 +113,7 @@ describe('rdme changelogs (single)', () => {
help: 'If you need help, email [email protected] and mention log "fake-metrics-uuid".',
};

const getMock = getAPIMock().get(`/api/v1/changelogs/${slug}`).basicAuth({ user: key }).reply(500, errorObject);
const getMock = getAPIV1Mock().get(`/api/v1/changelogs/${slug}`).basicAuth({ user: key }).reply(500, errorObject);

const filePath = `./__tests__/${fixturesBaseDir}/failure-docs/${slug}.md`;

Expand All @@ -135,7 +135,7 @@ describe('rdme changelogs (single)', () => {
const doc = frontMatter(fs.readFileSync(path.join(fullFixturesDir, `/slug-docs/${slug}.md`)));
const hash = hashFileContents(fs.readFileSync(path.join(fullFixturesDir, `/slug-docs/${slug}.md`)));

const getMock = getAPIMock()
const getMock = getAPIV1Mock()
.get(`/api/v1/changelogs/${doc.data.slug}`)
.basicAuth({ user: key })
.reply(404, {
Expand All @@ -145,7 +145,7 @@ describe('rdme changelogs (single)', () => {
help: 'If you need help, email [email protected] and mention log "fake-metrics-uuid".',
});

const postMock = getAPIMock()
const postMock = getAPIV1Mock()
.post('/api/v1/changelogs', { slug, body: doc.content, ...doc.data, lastUpdatedHash: hash })
.basicAuth({ user: key })
.reply(201, { slug: doc.data.slug, _id: id, body: doc.content, ...doc.data, lastUpdatedHash: hash });
Expand All @@ -172,12 +172,12 @@ describe('rdme changelogs (single)', () => {
});

it('should fetch changelog and merge with what is returned', () => {
const getMock = getAPIMock()
const getMock = getAPIV1Mock()
.get('/api/v1/changelogs/simple-doc')
.basicAuth({ user: key })
.reply(200, { slug: simpleDoc.slug, lastUpdatedHash: 'anOldHash' });

const updateMock = getAPIMock()
const updateMock = getAPIV1Mock()
.put('/api/v1/changelogs/simple-doc', {
body: simpleDoc.doc.content,
lastUpdatedHash: simpleDoc.hash,
Expand All @@ -202,7 +202,7 @@ describe('rdme changelogs (single)', () => {
it('should return changelog update info for dry run', () => {
expect.assertions(1);

const getMock = getAPIMock()
const getMock = getAPIV1Mock()
.get('/api/v1/changelogs/simple-doc')
.basicAuth({ user: key })
.reply(200, { slug: simpleDoc.slug, lastUpdatedHash: 'anOldHash' });
Expand All @@ -227,7 +227,7 @@ describe('rdme changelogs (single)', () => {
it('should not send requests for changelogs that have not changed', () => {
expect.assertions(1);

const getMock = getAPIMock()
const getMock = getAPIV1Mock()
.get('/api/v1/changelogs/simple-doc')
.basicAuth({ user: key })
.reply(200, { slug: simpleDoc.slug, lastUpdatedHash: simpleDoc.hash });
Expand All @@ -240,7 +240,7 @@ describe('rdme changelogs (single)', () => {
});

it('should adjust "no changes" message if in dry run', () => {
const getMock = getAPIMock()
const getMock = getAPIV1Mock()
.get('/api/v1/changelogs/simple-doc')
.basicAuth({ user: key })
.reply(200, { slug: simpleDoc.slug, lastUpdatedHash: simpleDoc.hash });
Expand Down
Loading

0 comments on commit 932a6d4

Please sign in to comment.