Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add categories and categories:create #530

Merged
merged 37 commits into from
Jul 21, 2022
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
312ddcb
updates
garrett-wade Jun 28, 2022
a6a1c6c
add categories
garrett-wade Jun 28, 2022
46fb666
updated test coverage and code
garrett-wade Jun 29, 2022
7f7b784
add better formated json out
garrett-wade Jun 29, 2022
0a03e5c
type
garrett-wade Jun 29, 2022
a7d97ec
readme updates
garrett-wade Jun 29, 2022
42b1cc1
updated readme
garrett-wade Jun 29, 2022
ac30f69
Merge branch 'main' into pr/530
kanadgupta Jun 30, 2022
be19af9
udpdate package lock
garrett-wade Jul 2, 2022
78039d8
Update README.md
garrett-wade Jul 5, 2022
6107075
update to npm@7
garrett-wade Jul 5, 2022
9104bf4
Update README.md
garrett-wade Jul 5, 2022
7ba5ad8
update `perPage` to 20
garrett-wade Jul 5, 2022
86e1158
Merge branch 'categories' of https://github.com/garrett-wade/rdme int…
garrett-wade Jul 5, 2022
6db611e
add resolve and improve test cover for 20 per page
garrett-wade Jul 5, 2022
8a6e6f1
move categories above utilities in commands
garrett-wade Jul 5, 2022
2d40fa4
update total conunt calc
garrett-wade Jul 5, 2022
bfd9af8
Merge branch 'main' into categories
garrett-wade Jul 11, 2022
22b04ec
- Update matching to exact title-matching
garrett-wade Jul 11, 2022
8ee2171
Update src/cmds/categories/create.js
garrett-wade Jul 11, 2022
48820b5
update READEM
garrett-wade Jul 11, 2022
0b93e68
Update src/cmds/categories/create.js
garrett-wade Jul 11, 2022
1b4ce63
only get all categories if the `preventDuplicates` flag is checked
garrett-wade Jul 11, 2022
7670779
Merge branch 'categories' of https://github.com/garrett-wade/rdme int…
garrett-wade Jul 11, 2022
6151c47
Update src/cmds/categories/index.js
garrett-wade Jul 11, 2022
27a70a9
update test coverage
garrett-wade Jul 11, 2022
3005818
Merge branch 'categories' of https://github.com/garrett-wade/rdme int…
garrett-wade Jul 11, 2022
20e94a3
change matching logic
garrett-wade Jul 18, 2022
cb8070c
add test coverage for title matching change
garrett-wade Jul 18, 2022
289ce05
replace `filter` with `find`
garrett-wade Jul 18, 2022
06912a8
reject promise if a category is not created
garrett-wade Jul 18, 2022
ed0860c
chore: minor copy edits
kanadgupta Jul 21, 2022
d9e0eef
chore: fix package and lockfiles
kanadgupta Jul 21, 2022
64c764b
test: stricter error checks, slight test renames
kanadgupta Jul 21, 2022
86b8341
refactor: consolidate logic, prevent unnecessary API calls
kanadgupta Jul 21, 2022
46e236b
chore: remove unnecessary catch statements
kanadgupta Jul 21, 2022
275b700
Merge branch 'main' into pr/530
kanadgupta Jul 21, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,24 @@ If you are logged in, this will open the project in your browser:
rdme open
```

### Categories

#### Get All Categories Associated to Your Project Version

```sh
rdme categories --version={project-version}
```

#### Create a New Category for your Project Version

You can create a new category with a specified `title` and `type` for your project version. The `title`
will be converted to a slug in param case. If a category already exists with the same slug and type for
garrett-wade marked this conversation as resolved.
Show resolved Hide resolved
the specified version then a new category is not created and the id of the existing category is returned. This includes if the slug contains incremented suffix such as `-1` or `-2`. If there are multiple categories with a matching slug and type then only one matching category is returned.
garrett-wade marked this conversation as resolved.
Show resolved Hide resolved

```sh
rdme categories:create <title> --categoryType ={category-type} --version={project-version}
garrett-wade marked this conversation as resolved.
Show resolved Hide resolved
```

## Future

We are continually expanding and improving the offerings of this application as we expand our public API and are able. Some interactions may change over time, but we will do our best to retain backwards compatibility.
177 changes: 177 additions & 0 deletions __tests__/cmds/categories.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
const nock = require('nock');

const getApiNock = require('../get-api-nock');

const CategoriesCommand = require('../../src/cmds/categories');
const CategoriesCreateCommand = require('../../src/cmds/categories/create');

const categories = new CategoriesCommand();
const categoriesCreate = new CategoriesCreateCommand();

const key = 'API_KEY';
const version = '1.0.0';

function getNockWithVersionHeader(v) {
return getApiNock({
'x-readme-version': v,
});
}

describe('rdme categories', () => {
beforeAll(() => nock.disableNetConnect());

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

it('should error if no api key provided', () => {
return expect(categories.run({})).rejects.toThrow('No project API key provided. Please use `--key`.');
});

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

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

await expect(categories.run({ key, version: '1.0.0' })).resolves.toBe(
JSON.stringify([{ title: 'One Category', slug: 'one-category', type: 'guide' }], null, 2)
);

getMock.done();
versionMock.done();
});

it('should return all categories for multiple pages', async () => {
const getMock = getNockWithVersionHeader(version)
.persist()
.get('/api/v1/categories?perPage=10&page=1')
.basicAuth({ user: key })
.reply(200, [{ title: 'One Category', slug: 'one-category', type: 'guide' }], {
'x-total-count': '17',
})
.get('/api/v1/categories?perPage=10&page=2')
.basicAuth({ user: key })
.reply(200, [{ title: 'Another Category', slug: 'another-category', type: 'guide' }], {
'x-total-count': '17',
});

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

await expect(categories.run({ key, version: '1.0.0' })).resolves.toBe(
JSON.stringify(
[
{ title: 'One Category', slug: 'one-category', type: 'guide' },
{ title: 'Another Category', slug: 'another-category', type: 'guide' },
],
null,
2
)
);

getMock.done();
versionMock.done();
});
});

describe('rdme categories:create', () => {
beforeAll(() => nock.disableNetConnect());

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

it('should error if no api key provided', () => {
return expect(categoriesCreate.run({})).rejects.toThrow('No project API key provided. Please use `--key`.');
});

it('should error if no title provided', () => {
return expect(categoriesCreate.run({ key: '123' })).rejects.toThrow(
'No title provided. Usage `rdme categories:create <title> [options]`.'
);
});

it('should error if categoryType is blank', () => {
return expect(categoriesCreate.run({ key: '123', title: 'Test Title' })).rejects.toThrow(
'`categoryType` must be guide or reference.'
);
});

it('should error if categoryType is not `guide` or `reference`', () => {
return expect(categoriesCreate.run({ key: '123', title: 'Test Title', categoryType: 'test' })).rejects.toThrow(
'`categoryType` must be guide or reference.'
);
});

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

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

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

await expect(
categoriesCreate.run({ title: 'New Category', categoryType: 'guide', key, version: '1.0.0' })
).resolves.toBe("🌱 successfully created 'new-category' with a type of 'guide' and an id of '123'");

getMock.done();
postMock.done();
versionMock.done();
});

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

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

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

await expect(
categoriesCreate.run({ title: 'Category', categoryType: 'reference', key, version: '1.0.0' })
).resolves.toBe("🌱 successfully created 'category' with a type of 'reference' and an id of '123'");

getMock.done();
postMock.done();
versionMock.done();
});

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

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

await expect(
categoriesCreate.run({ title: 'Category', categoryType: 'guide', key, version: '1.0.0' })
).resolves.toBe(
"The 'category' category with a type of 'guide' already exists with an id of '123'. A new category was not created"
);

getMock.done();
versionMock.done();
});
});
Loading