Yuki Takei 2 лет назад
Родитель
Сommit
dc83c0fa9f

+ 88 - 0
packages/remark-lsx/src/server/routes/list-pages/add-num-condition.spec.ts

@@ -0,0 +1,88 @@
+import type { IPage } from '@growi/core';
+import { OptionParser } from '@growi/core/dist/plugin';
+import createError from 'http-errors';
+import type { Query, Document } from 'mongoose';
+import { mock } from 'vitest-mock-extended';
+
+import { addNumCondition } from './add-num-condition';
+
+describe('addNumCondition()', () => {
+
+  const queryMock = mock<Query<IPage[], Document>>();
+
+  it('throws 400 http-errors instance when the option value is null', () => {
+    // when
+    const caller = () => addNumCondition(queryMock, null);
+
+    // then
+    expect(caller).toThrowError(createError(400, 'The value of num option is invalid.'));
+  });
+
+  it('throws 400 http-errors instance when the option value is true', () => {
+    // when
+    const caller = () => addNumCondition(queryMock, true);
+
+    // then
+    expect(caller).toThrowError(createError(400, 'The value of num option is invalid.'));
+  });
+
+  it('set limit with the specified number', () => {
+    // setup
+    const parseRangeSpy = vi.spyOn(OptionParser, 'parseRange');
+
+    const queryLimitResultMock = mock<Query<IPage[], Document>>();
+    queryMock.limit.calledWith(99).mockImplementation(() => queryLimitResultMock);
+
+    // when
+    const result = addNumCondition(queryMock, 99);
+
+    // then
+    expect(queryMock.limit).toHaveBeenCalledWith(99);
+    expect(result).toEqual(queryLimitResultMock);
+    expect(parseRangeSpy).not.toHaveBeenCalled();
+  });
+
+  it('returns the specified qeury as-is when the option value is invalid', () => {
+    // setup
+    const parseRangeSpy = vi.spyOn(OptionParser, 'parseRange');
+
+    // when
+    const result = addNumCondition(queryMock, 'invalid string');
+
+    // then
+    expect(queryMock.limit).not.toHaveBeenCalledWith(99);
+    expect(parseRangeSpy).toHaveBeenCalledWith('invalid string');
+    expect(result).toEqual(queryMock);
+  });
+
+});
+
+
+describe('addNumCondition() set skip and limit with the range string', () => {
+
+  it.concurrent.each`
+    optionsNum    | expectedSkip    | expectedLimit
+    ${'1:10'}     | ${0}            | ${10}
+  `("'$optionsNum", ({ optionsNum, expectedSkip, expectedLimit }) => {
+    // setup
+    const queryMock = mock<Query<IPage[], Document>>();
+
+    const querySkipResultMock = mock<Query<IPage[], Document>>();
+    queryMock.skip.calledWith(expectedSkip).mockImplementation(() => querySkipResultMock);
+
+    const queryLimitResultMock = mock<Query<IPage[], Document>>();
+    querySkipResultMock.limit.calledWith(expectedLimit).mockImplementation(() => queryLimitResultMock);
+
+    const parseRangeSpy = vi.spyOn(OptionParser, 'parseRange');
+
+    // when
+    const result = addNumCondition(queryMock, optionsNum);
+
+    // then
+    expect(parseRangeSpy).toHaveBeenCalledWith(optionsNum);
+    expect(queryMock.skip).toHaveBeenCalledWith(expectedSkip);
+    expect(querySkipResultMock.limit).toHaveBeenCalledWith(expectedLimit);
+    expect(result).toEqual(queryLimitResultMock);
+  });
+
+});

+ 1 - 1
packages/remark-lsx/src/server/routes/list-pages/add-num-condition.ts

@@ -8,7 +8,7 @@ import type { Query, Document } from 'mongoose';
  * add num condition that limit fetched pages
  * add num condition that limit fetched pages
  */
  */
 export const addNumCondition = (query: Query<IPage[], Document>, optionsNum: true | string | number | null): Query<IPage[], Document> => {
 export const addNumCondition = (query: Query<IPage[], Document>, optionsNum: true | string | number | null): Query<IPage[], Document> => {
-  // when option strings is 'num=', the option value is true
+  // when option strings is 'num=' or the option value is true
   if (optionsNum == null || optionsNum === true) {
   if (optionsNum == null || optionsNum === true) {
     throw createError(400, 'The value of num option is invalid.');
     throw createError(400, 'The value of num option is invalid.');
   }
   }