Просмотр исходного кода

feat: add unit tests for addFilterCondition function

Shun Miyazawa 7 месяцев назад
Родитель
Сommit
5c7c92e83c

+ 85 - 1
packages/remark-lsx/src/server/routes/list-pages/index.spec.ts

@@ -4,7 +4,7 @@ import createError from 'http-errors';
 import { mock } from 'vitest-mock-extended';
 
 import type { LsxApiParams, LsxApiResponseData } from '../../../interfaces/api';
-import { listPages } from '.';
+import { addFilterCondition, listPages } from '.';
 import type { PageQuery, PageQueryBuilder } from './generate-base-query';
 
 interface IListPagesRequest
@@ -158,4 +158,88 @@ describe('listPages', () => {
       expect(resStatusMock.send).toHaveBeenCalledWith('error for test');
     });
   });
+
+  describe('addFilterCondition', () => {
+    const queryMock = mock<PageQuery>();
+    // and method returns mock itself
+    queryMock.and.mockReturnValue(queryMock);
+
+    beforeEach(() => {
+      queryMock.and.mockClear();
+    });
+
+    it('should call query.and with the correct regex when filter starts with "^"', () => {
+      // setup
+      const pagePath = '/parent';
+      const optionsFilter = '^child';
+      const expectedRegex = /^\/parent\/child/;
+
+      // when
+      addFilterCondition(queryMock, pagePath, optionsFilter);
+
+      // then
+      expect(queryMock.and).toHaveBeenCalledWith({ path: expectedRegex });
+    });
+
+    it('should call query.and with the correct regex when filter does not start with "^"', () => {
+      // setup
+      const pagePath = '/parent';
+      const optionsFilter = 'child';
+      const expectedRegex = /^\/parent\/.*child/;
+
+      // when
+      addFilterCondition(queryMock, pagePath, optionsFilter);
+
+      // then
+      expect(queryMock.and).toHaveBeenCalledWith({ path: expectedRegex });
+    });
+
+    it('should properly escape regex meta-characters like "[" in filter', () => {
+      // setup
+      const pagePath = '/parent';
+      const optionsFilter = '['; // Invalid regex
+      const expectedRex = /^\/parent\/.*\[/;
+
+      // when
+      addFilterCondition(queryMock, pagePath, optionsFilter);
+
+      // then
+      expect(queryMock.and).toHaveBeenCalledWith({ path: expectedRex });
+    });
+
+    it('should call query.and with "$not" when isExceptFilter is true', () => {
+      // setup
+      const pagePath = '/parent';
+      const optionsFilter = 'child';
+      const expectedRegex = /^\/parent\/.*child/;
+
+      // when
+      addFilterCondition(queryMock, pagePath, optionsFilter, true);
+
+      // then
+      expect(queryMock.and).toHaveBeenCalledWith({
+        path: { $not: expectedRegex },
+      });
+    });
+
+    it('should throw an error when optionsFilter is null', () => {
+      // setup
+      const pagePath = '/parent';
+
+      // when & then
+      expect(() => addFilterCondition(queryMock, pagePath, null)).toThrow(
+        createError(400, 'filter option require value in regular expression.'),
+      );
+    });
+
+    it('should throw an error when optionsFilter is true', () => {
+      // setup
+      const pagePath = '/parent';
+
+      // when & then
+      expect(() => addFilterCondition(queryMock, pagePath, true)).toThrow(
+        createError(400, 'filter option require value in regular expression.'),
+      );
+    });
+  });
 });

+ 1 - 1
packages/remark-lsx/src/server/routes/list-pages/index.ts

@@ -18,7 +18,7 @@ const { addTrailingSlash, removeTrailingSlash } = pathUtils;
 /**
  * add filter condition that filter fetched pages
  */
-function addFilterCondition(
+export function addFilterCondition(
   query,
   pagePath,
   optionsFilter,