|
@@ -1,9 +1,11 @@
|
|
|
-import { IPage, IUser } from '@growi/core';
|
|
|
|
|
|
|
+import { IPageHasId, IUser } from '@growi/core';
|
|
|
import type { Request, Response } from 'express';
|
|
import type { Request, Response } from 'express';
|
|
|
import createError from 'http-errors';
|
|
import createError from 'http-errors';
|
|
|
import { mock } from 'vitest-mock-extended';
|
|
import { mock } from 'vitest-mock-extended';
|
|
|
|
|
|
|
|
-import type { PageQuery } from './generate-base-query';
|
|
|
|
|
|
|
+import type { LsxApiResponseData } from '../../../interfaces/api';
|
|
|
|
|
+
|
|
|
|
|
+import type { PageQuery, PageQueryBuilder } from './generate-base-query';
|
|
|
|
|
|
|
|
import { listPages } from '.';
|
|
import { listPages } from '.';
|
|
|
|
|
|
|
@@ -44,16 +46,30 @@ describe('listPages', () => {
|
|
|
|
|
|
|
|
describe('with num option', () => {
|
|
describe('with num option', () => {
|
|
|
|
|
|
|
|
- mocks.generateBaseQueryMock.mockImplementation(() => vi.fn());
|
|
|
|
|
|
|
+ const reqMock = mock<Request & { user: IUser }>();
|
|
|
|
|
+ reqMock.query = { pagePath: '/Sandbox' };
|
|
|
|
|
+
|
|
|
|
|
+ const builderMock = mock<PageQueryBuilder>();
|
|
|
|
|
+
|
|
|
|
|
+ mocks.generateBaseQueryMock.mockResolvedValue(builderMock);
|
|
|
mocks.getToppageViewersCountMock.mockImplementation(() => 99);
|
|
mocks.getToppageViewersCountMock.mockImplementation(() => 99);
|
|
|
|
|
|
|
|
|
|
+ const queryMock = mock<PageQuery>();
|
|
|
|
|
+ builderMock.query = queryMock;
|
|
|
|
|
+
|
|
|
it('returns 200 HTTP response', async() => {
|
|
it('returns 200 HTTP response', async() => {
|
|
|
- // setup
|
|
|
|
|
- const reqMock = mock<Request & { user: IUser }>();
|
|
|
|
|
- reqMock.query = { pagePath: '/Sandbox' };
|
|
|
|
|
|
|
+ // setup query.clone().count()
|
|
|
|
|
+ const queryClonedMock = mock<PageQuery>();
|
|
|
|
|
+ queryMock.clone.mockImplementation(() => queryClonedMock);
|
|
|
|
|
+ queryClonedMock.count.mockResolvedValue(9);
|
|
|
|
|
+
|
|
|
|
|
+ // setup addNumCondition
|
|
|
|
|
+ mocks.addNumConditionMock.mockImplementation(() => queryMock);
|
|
|
|
|
+ // setup addSortCondition
|
|
|
|
|
+ mocks.addSortConditionMock.mockImplementation(() => queryMock);
|
|
|
|
|
|
|
|
- const pageMock = mock<IPage>();
|
|
|
|
|
- const queryMock = mock<PageQuery>();
|
|
|
|
|
|
|
+ // setup query.exec()
|
|
|
|
|
+ const pageMock = mock<IPageHasId>();
|
|
|
queryMock.exec.mockImplementation(async() => [pageMock]);
|
|
queryMock.exec.mockImplementation(async() => [pageMock]);
|
|
|
mocks.addSortConditionMock.mockImplementation(() => queryMock);
|
|
mocks.addSortConditionMock.mockImplementation(() => queryMock);
|
|
|
|
|
|
|
@@ -70,10 +86,13 @@ describe('listPages', () => {
|
|
|
expect(mocks.addNumConditionMock).toHaveBeenCalledOnce();
|
|
expect(mocks.addNumConditionMock).toHaveBeenCalledOnce();
|
|
|
expect(mocks.addSortConditionMock).toHaveBeenCalledOnce();
|
|
expect(mocks.addSortConditionMock).toHaveBeenCalledOnce();
|
|
|
expect(resMock.status).toHaveBeenCalledOnce();
|
|
expect(resMock.status).toHaveBeenCalledOnce();
|
|
|
- expect(resStatusMock.send).toHaveBeenCalledWith({
|
|
|
|
|
|
|
+ const expectedResponseData: LsxApiResponseData = {
|
|
|
pages: [pageMock],
|
|
pages: [pageMock],
|
|
|
|
|
+ cursor: 1,
|
|
|
|
|
+ total: 9,
|
|
|
toppageViewersCount: 99,
|
|
toppageViewersCount: 99,
|
|
|
- });
|
|
|
|
|
|
|
+ };
|
|
|
|
|
+ expect(resStatusMock.send).toHaveBeenCalledWith(expectedResponseData);
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
it('returns 500 HTTP response when an unexpected error occured', async() => {
|
|
it('returns 500 HTTP response when an unexpected error occured', async() => {
|
|
@@ -82,9 +101,9 @@ describe('listPages', () => {
|
|
|
reqMock.query = { pagePath: '/Sandbox' };
|
|
reqMock.query = { pagePath: '/Sandbox' };
|
|
|
|
|
|
|
|
// an Error instance will be thrown by addNumConditionMock
|
|
// an Error instance will be thrown by addNumConditionMock
|
|
|
- const expectedError = new Error('error for test');
|
|
|
|
|
|
|
+ const error = new Error('error for test');
|
|
|
mocks.addNumConditionMock.mockImplementation(() => {
|
|
mocks.addNumConditionMock.mockImplementation(() => {
|
|
|
- throw expectedError;
|
|
|
|
|
|
|
+ throw error;
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
const resMock = mock<Response>();
|
|
const resMock = mock<Response>();
|
|
@@ -100,7 +119,7 @@ describe('listPages', () => {
|
|
|
expect(mocks.addNumConditionMock).toHaveBeenCalledOnce(); // throw an error
|
|
expect(mocks.addNumConditionMock).toHaveBeenCalledOnce(); // throw an error
|
|
|
expect(mocks.addSortConditionMock).not.toHaveBeenCalledOnce(); // does not called
|
|
expect(mocks.addSortConditionMock).not.toHaveBeenCalledOnce(); // does not called
|
|
|
expect(resMock.status).toHaveBeenCalledOnce();
|
|
expect(resMock.status).toHaveBeenCalledOnce();
|
|
|
- expect(resStatusMock.send).toHaveBeenCalledWith(expectedError);
|
|
|
|
|
|
|
+ expect(resStatusMock.send).toHaveBeenCalledWith('error for test');
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
it('returns 400 HTTP response when the value is invalid', async() => {
|
|
it('returns 400 HTTP response when the value is invalid', async() => {
|
|
@@ -109,9 +128,9 @@ describe('listPages', () => {
|
|
|
reqMock.query = { pagePath: '/Sandbox' };
|
|
reqMock.query = { pagePath: '/Sandbox' };
|
|
|
|
|
|
|
|
// an http-errors instance will be thrown by addNumConditionMock
|
|
// an http-errors instance will be thrown by addNumConditionMock
|
|
|
- const expectedError = createError(400, 'error for test');
|
|
|
|
|
|
|
+ const error = createError(400, 'error for test');
|
|
|
mocks.addNumConditionMock.mockImplementation(() => {
|
|
mocks.addNumConditionMock.mockImplementation(() => {
|
|
|
- throw expectedError;
|
|
|
|
|
|
|
+ throw error;
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
const resMock = mock<Response>();
|
|
const resMock = mock<Response>();
|
|
@@ -127,7 +146,7 @@ describe('listPages', () => {
|
|
|
expect(mocks.addNumConditionMock).toHaveBeenCalledOnce(); // throw an error
|
|
expect(mocks.addNumConditionMock).toHaveBeenCalledOnce(); // throw an error
|
|
|
expect(mocks.addSortConditionMock).not.toHaveBeenCalledOnce(); // does not called
|
|
expect(mocks.addSortConditionMock).not.toHaveBeenCalledOnce(); // does not called
|
|
|
expect(resMock.status).toHaveBeenCalledOnce();
|
|
expect(resMock.status).toHaveBeenCalledOnce();
|
|
|
- expect(resStatusMock.send).toHaveBeenCalledWith(expectedError);
|
|
|
|
|
|
|
+ expect(resStatusMock.send).toHaveBeenCalledWith('error for test');
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
});
|
|
});
|