Skip to content

Commit

Permalink
refactor: 181/184 unit test suite passing
Browse files Browse the repository at this point in the history
  • Loading branch information
Xunnamius committed Jun 1, 2023
1 parent dd6e752 commit a70d563
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 313 deletions.
4 changes: 2 additions & 2 deletions src/pages/api/v1/blogs/[blogName]/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { withMiddleware } from 'universe/backend/middleware';
import { getBlogPagesMetadata, updateBlog } from 'universe/backend';
import { getBlog, updateBlog } from 'universe/backend';
import { sendHttpOk } from 'multiverse/next-api-respond';

// ? This is a NextJS special "config" export
Expand All @@ -16,7 +16,7 @@ export default withMiddleware(
switch (req.method) {
case 'GET': {
sendHttpOk(res, {
blog: await getBlogPagesMetadata({ blogName })
blog: await getBlog({ blogName })
});
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,24 @@ export default withMiddleware(

switch (req.method) {
case 'PUT': {
await renewSession({ session_id });
await renewSession({ sessionId: session_id });
sendHttpOk(res);
break;
}

case 'DELETE': {
await deleteSession({ session_id });
await deleteSession({ sessionId: session_id });
sendHttpOk(res);
break;
}
}
},
{
descriptor: metadata.descriptor,
options: { allowedMethods: ['PUT', 'DELETE'], apiVersion: '1' }
options: {
allowedContentTypes: { PUT: ['application/json', 'none'], DELETE: 'none' },
allowedMethods: ['PUT', 'DELETE'],
apiVersion: '1'
}
}
);
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { withMiddleware } from 'universe/backend/middleware';
import { createSession, getBlogPagesMetadata } from 'universe/backend';
import { createSession, getPageSessionsCount } from 'universe/backend';
import { authorizationHeaderToOwnerAttribute } from 'universe/backend/api';
import { sendHttpOk } from 'multiverse/next-api-respond';

Expand All @@ -18,7 +18,7 @@ export default withMiddleware(
switch (req.method) {
case 'GET': {
sendHttpOk(res, {
active: await getBlogPagesMetadata({ blogName })
active: await getPageSessionsCount({ blogName, pageName })
});
break;
}
Expand All @@ -39,6 +39,10 @@ export default withMiddleware(
},
{
descriptor: metadata.descriptor,
options: { allowedMethods: ['GET', 'POST'], apiVersion: '1' }
options: {
allowedContentTypes: { POST: ['application/json', 'none'], GET: 'none' },
allowedMethods: ['GET', 'POST'],
apiVersion: '1'
}
}
);
141 changes: 87 additions & 54 deletions test/api/unit-app-blogs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ import { testApiHandler } from 'next-test-api-route-handler';
import { api, setupMockBackend } from 'testverse/util';

jest.mock('universe/backend');
jest.mock('universe/backend/api', (): typeof import('universe/backend/api') => {
return {
...jest.requireActual('universe/backend/api'),
authorizationHeaderToOwnerAttribute: jest.fn(() => Promise.resolve('mock-owner'))
};
});

jest.mock(
'universe/backend/middleware',
(): typeof import('universe/backend/middleware') => {
Expand All @@ -17,136 +24,142 @@ jest.mock(
}
);

const { mockedAuthAppUser } = setupMockBackend();
setupMockBackend();

describe('api/v1/users', () => {
describe('api/v1/blogs/:blogName', () => {
describe('/ [GET]', () => {
it('accepts GET requests', async () => {
expect.hasAssertions();

await testApiHandler({
handler: api.v1.users,
handler: api.v1.blogsBlogname,
params: { blogName: 'blog-name' },
test: async ({ fetch }) => {
const [status, json] = await fetch({ method: 'GET' }).then(
async (r) => [r.status, await r.json()] as [status: number, json: any]
);

expect(status).toBe(200);
expect(json.success).toBeTrue();
expect(json.users).toBeArray();
expect(json.blog).toBeObject();
expect(Object.keys(json)).toHaveLength(2);
}
});
});
});

describe('/ [POST]', () => {
it('accepts POST requests', async () => {
describe('/ [PATCH]', () => {
it('accepts PATCH requests', async () => {
expect.hasAssertions();

await testApiHandler({
handler: api.v1.users,
handler: api.v1.blogsBlogname,
params: { blogName: 'blog-name' },
test: async ({ fetch }) => {
const [status, json] = await fetch({ method: 'POST' }).then(
const [status, json] = await fetch({ method: 'PATCH' }).then(
async (r) => [r.status, await r.json()] as [status: number, json: any]
);

expect(status).toBe(200);
expect(json.success).toBeTrue();
expect(json.user).toBeObject();
expect(Object.keys(json)).toHaveLength(2);
expect(Object.keys(json)).toHaveLength(1);
}
});
});
});

describe('/:username [GET]', () => {
describe('/pages [GET]', () => {
it('accepts GET requests', async () => {
expect.hasAssertions();

await testApiHandler({
handler: api.v1.usersUsername,
params: { username: 'User1' },
handler: api.v1.blogsBlognamePages,
params: { blogName: 'blog-name' },
test: async ({ fetch }) => {
const [status, json] = await fetch({ method: 'GET' }).then(
async (r) => [r.status, await r.json()] as [status: number, json: any]
);

expect(status).toBe(200);
expect(json.success).toBeTrue();
expect(json.user).toBeObject();
expect(json.pages).toBeArray();
expect(Object.keys(json)).toHaveLength(2);
}
});
});
});

describe('/:username [PATCH]', () => {
it('accepts PATCH requests', async () => {
describe('/pages [POST]', () => {
it('accepts POST requests', async () => {
expect.hasAssertions();

await testApiHandler({
handler: api.v1.usersUsername,
params: { username: 'User1' },
handler: api.v1.blogsBlognamePages,
params: { blogName: 'blog-name' },
test: async ({ fetch }) => {
const [status, json] = await fetch({ method: 'PATCH' }).then(
const [status, json] = await fetch({ method: 'POST' }).then(
async (r) => [r.status, await r.json()] as [status: number, json: any]
);

expect(status).toBe(200);
expect(json.success).toBeTrue();
expect(Object.keys(json)).toHaveLength(1);
expect(json.page).toBeObject();
expect(Object.keys(json)).toHaveLength(2);
}
});
});
});

describe('/:username [DELETE]', () => {
it('accepts DELETE requests', async () => {
describe('/pages/:pageName [GET]', () => {
it('accepts GET requests', async () => {
expect.hasAssertions();

await testApiHandler({
handler: api.v1.usersUsername,
params: { username: 'User1' },
handler: api.v1.blogsBlognamePagesPagename,
params: { blogName: 'blog-name', pageName: 'page-name' },
test: async ({ fetch }) => {
const [status, json] = await fetch({ method: 'DELETE' }).then(
const [status, json] = await fetch({ method: 'GET' }).then(
async (r) => [r.status, await r.json()] as [status: number, json: any]
);

expect(status).toBe(200);
expect(json.success).toBeTrue();
expect(Object.keys(json)).toHaveLength(1);
expect(json.page).toBeObject();
expect(Object.keys(json)).toHaveLength(2);
}
});
});
});

describe('/:username/auth [POST]', () => {
it('accepts POST requests', async () => {
describe('/pages/:pageName [PATCH]', () => {
it('accepts PATCH requests', async () => {
expect.hasAssertions();

await testApiHandler({
handler: api.v1.usersUsernameAuth,
params: { username: 'User1' },
handler: api.v1.blogsBlognamePagesPagename,
params: { blogName: 'blog-name', pageName: 'page-name' },
test: async ({ fetch }) => {
const [status, json] = await fetch({ method: 'POST' }).then(
const [status, json] = await fetch({ method: 'PATCH' }).then(
async (r) => [r.status, await r.json()] as [status: number, json: any]
);

expect(status).toBe(403);
expect(json.success).toBeFalse();
expect(json.error).toBeString();
expect(Object.keys(json)).toHaveLength(2);
expect(status).toBe(200);
expect(json.success).toBeTrue();
expect(Object.keys(json)).toHaveLength(1);
}
});
});
});

mockedAuthAppUser.mockReturnValue(Promise.resolve(true));
describe('/pages/:pageName [DELETE]', () => {
it('accepts DELETE requests', async () => {
expect.hasAssertions();

await testApiHandler({
handler: api.v1.usersUsernameAuth,
params: { username: 'User1' },
handler: api.v1.blogsBlognamePagesPagename,
params: { blogName: 'blog-name', pageName: 'page-name' },
test: async ({ fetch }) => {
const [status, json] = await fetch({ method: 'POST' }).then(
const [status, json] = await fetch({ method: 'DELETE' }).then(
async (r) => [r.status, await r.json()] as [status: number, json: any]
);

Expand All @@ -158,57 +171,77 @@ describe('api/v1/users', () => {
});
});

describe('/:username/questions [GET]', () => {
describe('/pages/:pageName/sessions [GET]', () => {
it('accepts GET requests', async () => {
expect.hasAssertions();

await testApiHandler({
handler: api.v1.usersUsernameQuestions,
params: { username: 'User1' },
handler: api.v1.blogsBlognamePagesPagenameSessions,
params: { blogName: 'blog-name', pageName: 'page-name' },
test: async ({ fetch }) => {
const [status, json] = await fetch({ method: 'GET' }).then(
async (r) => [r.status, await r.json()] as [status: number, json: any]
);

expect(status).toBe(200);
expect(json.success).toBeTrue();
expect(json.questions).toBeArray();
expect(json.active).toBeNumber();
expect(Object.keys(json)).toHaveLength(2);
}
});
});
});

describe('/:username/answers [GET]', () => {
it('accepts GET requests', async () => {
describe('/pages/:pageName/sessions [POST]', () => {
it('accepts POST requests', async () => {
expect.hasAssertions();

await testApiHandler({
handler: api.v1.usersUsernameAnswers,
params: { username: 'User1' },
handler: api.v1.blogsBlognamePagesPagenameSessions,
params: { blogName: 'blog-name', pageName: 'page-name' },
test: async ({ fetch }) => {
const [status, json] = await fetch({ method: 'GET' }).then(
const [status, json] = await fetch({ method: 'POST' }).then(
async (r) => [r.status, await r.json()] as [status: number, json: any]
);

expect(status).toBe(200);
expect(json.success).toBeTrue();
expect(json.answers).toBeArray();
expect(json.session_id).toBeString();
expect(Object.keys(json)).toHaveLength(2);
}
});
});
});

describe('/:username/points [PATCH]', () => {
it('accepts PATCH requests', async () => {
describe('/pages/:pageName/sessions/:session_id [PUT]', () => {
it('accepts PUT requests', async () => {
expect.hasAssertions();

await testApiHandler({
handler: api.v1.usersUsernamePoints,
params: { username: 'User1' },
handler: api.v1.blogsBlognamePagesPagenameSessionsSessionid,
params: { blogName: 'blog-name', pageName: 'page-name', session_id: 'id' },
test: async ({ fetch }) => {
const [status, json] = await fetch({ method: 'PATCH' }).then(
const [status, json] = await fetch({ method: 'PUT' }).then(
async (r) => [r.status, await r.json()] as [status: number, json: any]
);

expect(status).toBe(200);
expect(json.success).toBeTrue();
expect(Object.keys(json)).toHaveLength(1);
}
});
});
});

describe('/pages/:pageName/sessions/:session_id [DELETE]', () => {
it('accepts DELETE requests', async () => {
expect.hasAssertions();

await testApiHandler({
handler: api.v1.blogsBlognamePagesPagenameSessionsSessionid,
params: { blogName: 'blog-name', pageName: 'page-name', session_id: 'id' },
test: async ({ fetch }) => {
const [status, json] = await fetch({ method: 'DELETE' }).then(
async (r) => [r.status, await r.json()] as [status: number, json: any]
);

Expand Down
Loading

0 comments on commit a70d563

Please sign in to comment.