Skip to content

Commit

Permalink
New: adds draft blog example
Browse files Browse the repository at this point in the history
  • Loading branch information
pustovitDmytro committed Feb 8, 2021
1 parent d7a94e2 commit b27f5cb
Show file tree
Hide file tree
Showing 6 changed files with 435 additions and 44 deletions.
8 changes: 8 additions & 0 deletions ava.config.js
@@ -0,0 +1,8 @@
export default {
'files' : [
'examples/blog/*test.js'
],
'require' : [
'@babel/register'
]
};
66 changes: 66 additions & 0 deletions examples/blog/app.js
@@ -0,0 +1,66 @@
import jsonServer from 'json-server';

const port = process.env.PORT || 3002;

const posts = [ {
'id' : 1,
'title' : 'sunt aut facere repellat provident occaecati excepturi optio reprehenderit',
'body' : 'quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto',
thumbnail : null
},
{
'id' : 2,
'title' : 'qui est esse',
'body' : 'est rerum tempore vitae\nsequi sint nihil reprehenderit dolor beatae ea dolores neque\nfugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis\nqui aperiam non debitis possimus qui neque nisi nulla',
thumbnail : 'https://via.placeholder.com/150/24f355'
},
{
'id' : 3,
'title' : 'ea molestias quasi exercitationem repellat qui ipsa sit aut',
'body' : 'et iusto sed quo iure\nvoluptatem occaecati omnis eligendi aut ad\nvoluptatem doloribus vel accusantium quis pariatur\nmolestiae porro eius odio et labore et velit aut',
thumbnail : null
} ];

const comments = [ {
'userId' : 1,
'id' : 1,
'title' : 'delectus aut autem',
'isEdited' : false
},
{
'userId' : 2,
'id' : 2,
'title' : 'quis ut nam facilis et officia qui',
'isEdited' : true
},
{
'userId' : 1,
'id' : 3,
'title' : 'fugiat veniam minus',
'isEdited' : false
},
{
'userId' : 5,
'id' : 4,
'title' : 'et porro tempora',
'isEdited' : true
},
{
'userId' : 2,
'id' : 5,
'title' : 'laboriosam mollitia et enim quasi adipisci quia provident illum',
'isEdited' : false
} ];

const server = jsonServer.create();
const router = jsonServer.router({ posts, comments });

server.use(router);

if (!process.env.TEST) {
server.listen(port, () => {
console.log(`Blog server is running on ${port}`);
});
}

export default server;
83 changes: 83 additions & 0 deletions examples/blog/blog.test.js
@@ -0,0 +1,83 @@
import test from 'ava';
import chronicle, { axios } from '../../src';
import express from './app';

const app = {};

function contextBuilder(context) {
return {
title : context.chronicle.title,
group : context.chronicle.group
};
}

test.before(async () => {
chronicle.setContextBuilder(contextBuilder);

await new Promise(res => {
const server = express.listen(0, () => {
const { port } = server.address();

app.port = port;
app.url = `http://localhost:${port}`;
app.server = server;
res();
});
});
});

test.beforeEach(t => {
t.context.chronicle = {
group : 'Posts',
title : t.title.replace('beforeEach hook for ', '')
};
});

test('Positive: show first post', async function (t) {
const res = await axios
.with(t.context)
.get(`${app.url}/posts/1`);

t.is(res.status, 200);
t.assert(res.data);
});

test('Positive: show single post by id', async function (t) {
const res = await axios
.with(t.context)
.get(`${app.url}/posts/:id`, { params: { id: 2 } });

t.is(res.status, 200);
t.assert(res.data);
});

test('Negative: post not found', async function (t) {
const res = await axios
.with(t.context)
.get(`${app.url}/posts/:id`, { params: { id: 16 } })
.catch(err => err);

t.is(res.response.status, 404);
});

test('Negative: bad id', async function (t) {
const res = await axios
.with(t.context)
.get(`${app.url}/posts/:id`, { params: { id: 'abcd' } })
.catch(err => err);

t.is(res.response.status, 404);
});

test.after('cleanup', async () => {
await chronicle.save('./documentation/swagger.json', {
reporter : 'swagger',
hash : action => action.context.title.replace(/\W/g, ' ').replace(/\s+/g, '_').toLowerCase()
});
chronicle.clear();
});

// after(async () => {
// await chronicle.save('./documentation/swagger.json', { reporter: 'swagger' });
// await chronicle.save('./documentation/api-blueprint.md', { reporter: 'api-blueprint' });
// });
216 changes: 216 additions & 0 deletions examples/blog/documentation/swagger.json
@@ -0,0 +1,216 @@
{
"openapi": "3.0.0",
"info": {
"version": "1.0.0",
"title": "Swagger Report"
},
"servers": [
{
"url": "http://localhost:44461"
}
],
"paths": {
"/posts/1": {
"get": {
"tags": [
"Posts"
],
"description": "Positive: show first post",
"parameters": [
{
"name": "Accept",
"in": "header",
"schema": {
"type": "string"
},
"example": "application/json, text/plain, */*"
},
{
"name": "User-Agent",
"in": "header",
"schema": {
"type": "string"
},
"example": "axios/0.21.1"
}
],
"responses": {
"200": {
"description": "Positive: show first post",
"content": {
"application/json": {
"schema": {
"type": "object",
"example": {
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto",
"thumbnail": null
},
"properties": {
"id": {
"type": "number",
"example": 1
},
"title": {
"type": "string",
"example": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit"
},
"body": {
"type": "string",
"example": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
},
"thumbnail": {
"type": "object",
"example": null
}
}
}
}
}
}
}
}
},
"/posts/:id": {
"get": {
"tags": [
"Posts"
],
"description": "Positive: show single post by id",
"parameters": [
{
"name": "Accept",
"in": "header",
"schema": {
"type": "string"
},
"example": "application/json, text/plain, */*"
},
{
"name": "User-Agent",
"in": "header",
"schema": {
"type": "string"
},
"example": "axios/0.21.1"
}
],
"responses": {
"200": {
"description": "Positive: show single post by id",
"content": {
"application/json": {
"schema": {
"type": "object",
"example": {
"id": 2,
"title": "qui est esse",
"body": "est rerum tempore vitae\nsequi sint nihil reprehenderit dolor beatae ea dolores neque\nfugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis\nqui aperiam non debitis possimus qui neque nisi nulla",
"thumbnail": "https://via.placeholder.com/150/24f355"
},
"properties": {
"id": {
"type": "number",
"example": 2
},
"title": {
"type": "string",
"example": "qui est esse"
},
"body": {
"type": "string",
"example": "est rerum tempore vitae\nsequi sint nihil reprehenderit dolor beatae ea dolores neque\nfugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis\nqui aperiam non debitis possimus qui neque nisi nulla"
},
"thumbnail": {
"type": "string",
"example": "https://via.placeholder.com/150/24f355"
}
}
}
}
}
}
}
}
},
"/posts/:id#negative_post_not_found": {
"get": {
"tags": [
"Posts"
],
"description": "Negative: post not found",
"parameters": [
{
"name": "Accept",
"in": "header",
"schema": {
"type": "string"
},
"example": "application/json, text/plain, */*"
},
{
"name": "User-Agent",
"in": "header",
"schema": {
"type": "string"
},
"example": "axios/0.21.1"
}
],
"responses": {
"404": {
"description": "Negative: post not found",
"content": {
"application/json": {
"schema": {
"type": "object",
"example": {}
}
}
}
}
}
}
},
"/posts/:id#negative_bad_id": {
"get": {
"tags": [
"Posts"
],
"description": "Negative: bad id",
"parameters": [
{
"name": "Accept",
"in": "header",
"schema": {
"type": "string"
},
"example": "application/json, text/plain, */*"
},
{
"name": "User-Agent",
"in": "header",
"schema": {
"type": "string"
},
"example": "axios/0.21.1"
}
],
"responses": {
"404": {
"description": "Negative: bad id",
"content": {
"application/json": {
"schema": {
"type": "object",
"example": {}
}
}
}
}
}
}
}
}
}

0 comments on commit b27f5cb

Please sign in to comment.