Skip to content

Commit

Permalink
Merge pull request #77 from SMAKSS/develop
Browse files Browse the repository at this point in the history
feat(core): add test for core functions
  • Loading branch information
SMAKSS authored Apr 8, 2024
2 parents 8961fa1 + 527c7cc commit 96d2f68
Show file tree
Hide file tree
Showing 13 changed files with 2,247 additions and 739 deletions.
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ lint-staged.config.cjs

# commitlint config
commitlint.config.cjs

# jest config
jest.config.cjs
5 changes: 0 additions & 5 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,16 @@
"es6": true
},
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended"],
// Specifying Parser
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module",
"tsconfigRootDir": ".",
"project": ["./tsconfig.json"]
},
// Configuring third-party plugins
"plugins": ["@typescript-eslint"],
// Resolve imports
"rules": {
"linebreak-style": "off",
// Disallow the `any` type.
"@typescript-eslint/no-explicit-any": "warn",
"@typescript-eslint/ban-types": [
"error",
Expand All @@ -29,7 +25,6 @@
}
],
"react-hooks/exhaustive-deps": "off",
// Enforce the use of the shorthand syntax.
"object-shorthand": "error",
"no-console": "warn"
}
Expand Down
11 changes: 7 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
fetch-depth: 1

Expand All @@ -19,12 +19,12 @@ jobs:
echo "Base ref: ${{ github.base_ref }}"
- name: Setup Node.js
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: "18"

- name: Cache dependencies
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: |
**/node_modules
Expand All @@ -35,6 +35,9 @@ jobs:
- name: Install dependencies
run: yarn install

- name: Run Tests
run: yarn test

- name: Type check
run: yarn typecheck

Expand All @@ -53,7 +56,7 @@ jobs:
run: yarn audit --summary

- name: Archive production artifacts
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: build-artifacts
path: ./build/
Expand Down
18 changes: 9 additions & 9 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 18
- run: yarn
- run: yarn generate
- uses: actions/upload-artifact@v3
- uses: actions/upload-artifact@v4
with:
name: built-package
path: |
Expand All @@ -25,11 +25,11 @@ jobs:
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/download-artifact@v3
- uses: actions/checkout@v4
- uses: actions/download-artifact@v4
with:
name: built-package
- uses: actions/setup-node@v3
- uses: actions/setup-node@v4
with:
node-version: 18
registry-url: https://registry.npmjs.org/
Expand All @@ -51,11 +51,11 @@ jobs:
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/download-artifact@v3
- uses: actions/checkout@v4
- uses: actions/download-artifact@v4
with:
name: built-package
- uses: actions/setup-node@v3
- uses: actions/setup-node@v4
with:
node-version: 18
registry-url: https://npm.pkg.github.com/
Expand Down
12 changes: 12 additions & 0 deletions jest.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
roots: ['<rootDir>/src'],
testMatch: [
'**/__tests__/**/*.+(ts|tsx|js)',
'**/?(*.)+(spec|test).+(ts|tsx|js)'
],
transform: {
'^.+\\.(ts|tsx)$': 'ts-jest'
}
};
30 changes: 18 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,24 @@
},
"description": "Enhance your searching capabilities with @smakss/search. Effortlessly find keywords in arrays, nested arrays, and objects, perfect for deep search needs in various data structures.",
"devDependencies": {
"@commitlint/cli": "^18.4.3",
"@commitlint/config-conventional": "^18.4.3",
"@commitlint/cli": "^19.2.1",
"@commitlint/config-conventional": "^19.1.0",
"@rollup/plugin-commonjs": "^25.0.7",
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-typescript": "^11.1.5",
"@typescript-eslint/eslint-plugin": "^6.16.0",
"@typescript-eslint/parser": "^6.16.0",
"@rollup/plugin-typescript": "^11.1.6",
"@types/jest": "^29.5.12",
"@types/node": "^20.12.5",
"@typescript-eslint/eslint-plugin": "^7.5.0",
"@typescript-eslint/parser": "^7.5.0",
"eslint": "^8.56.0",
"husky": "^8.0.3",
"lint-staged": "^15.2.0",
"prettier": "^3.1.1",
"rollup": "^4.9.1",
"husky": "^9.0.11",
"jest": "^29.7.0",
"lint-staged": "^15.2.2",
"prettier": "^3.2.5",
"rollup": "^4.14.1",
"ts-jest": "^29.1.2",
"tslib": "^2.6.2",
"typescript": "^5.3.3"
"typescript": "^5.4.4"
},
"engines": {
"node": ">=18.0.0"
Expand Down Expand Up @@ -67,9 +71,11 @@
"lint": "eslint --cache --cache-location ./node_modules/.cache/.eslintcache --ext js,ts --max-warnings=0 .",
"lint:fix": "eslint src/**/*.ts --fix",
"setup": "yarn && husky install",
"typecheck": "tsc -b ."
"test": "jest",
"typecheck": "tsc -b .",
"upgrade:deps": "rm -rf node_modules yarn.lock && ncu -u && yarn"
},
"type": "module",
"types": "lib",
"version": "2.0.1"
"version": "2.1.0"
}
8 changes: 7 additions & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@ export default [
sourcemap: true
}
],
plugins: [resolve(), commonjs(), typescript()],
plugins: [
resolve(),
commonjs(),
typescript({
exclude: ['**/*.test.ts', '**/*.spec.ts', '**/__tests__/**']
})
],
external: [
...Object.keys(packageJson.devDependencies || {}),
...Object.keys(packageJson.peerDependencies || {})
Expand Down
44 changes: 44 additions & 0 deletions src/search-functions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { searchWithinObject, recursiveSearch } from './search-functions'; // Adjust import path
import { SearchItem } from './types';

describe('searchWithinObject', () => {
it('should add object to results if a match is found', () => {
const person: SearchItem = { name: 'John', lastName: 'Doe' };
const results: SearchItem[] = [];
searchWithinObject(person, ['name'], true, /John/i, results);
expect(results).toContain(person);
});

it('should not add object to results if no match is found', () => {
const person: SearchItem = { name: 'John', lastName: 'Doe' };
const results: SearchItem[] = [];
searchWithinObject(person, ['name'], true, /Jane/i, results);
expect(results).toEqual([]);
});
});

describe('recursiveSearch', () => {
it('should add all matching items from an array', () => {
const people: SearchItem[] = [
{ name: 'John', lastName: 'Doe' },
{ name: 'Jane', lastName: 'Doe' }
];
const results: SearchItem[] = [];
recursiveSearch(people, ['lastName'], true, /Doe/i, results);
expect(results).toEqual(expect.arrayContaining(people));
});

it('should work recursively through nested arrays/objects', () => {
const nestedPeople: SearchItem[] = [
{
name: 'John',
lastName: 'Doe',
contacts: [{ name: 'Jake', lastName: 'Doe' }]
},
{ name: 'Jane', lastName: 'Doe' }
];
const results: SearchItem[] = [];
recursiveSearch(nestedPeople, ['lastName'], true, /Doe/i, results);
expect(results.length).toBe(2);
});
});
96 changes: 96 additions & 0 deletions src/search.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import search from './search';

describe('search', () => {
const people = [
{ name: 'John', lastName: 'Doe', age: 30 },
{ name: 'Jane', lastName: 'Smith', age: 25 },
{ name: 'Jake', lastName: 'Doe', age: 22 }
];

it('should find items by lastName', () => {
const options = {
searchText: 'doe',
searchItems: people,
keys: ['lastName'],
exact: false
};
const results = search(options);
expect(results.length).toBe(2);
expect(results).toEqual(
expect.arrayContaining([expect.objectContaining({ lastName: 'Doe' })])
);
});

it('should handle exact matches', () => {
const options = {
searchText: 'Doe',
searchItems: people,
keys: ['lastName'],
exact: true
};
const results = search(options);
expect(results.length).toBe(2);
expect(results).toEqual(
expect.arrayContaining([expect.objectContaining({ lastName: 'Doe' })])
);
});

it('should be case insensitive', () => {
const options = {
searchText: 'smith',
searchItems: people,
keys: ['lastName'],
exact: false
};
const results = search(options);
expect(results.length).toBe(1);
expect(results[0].lastName).toBe('Smith');
});

it('should return an empty array when no matches are found', () => {
const options = {
searchText: 'nonexistent',
searchItems: people,
keys: ['lastName'],
exact: false
};
const results = search(options);
expect(results.length).toBe(0);
});

it('should search all keys if none are specified', () => {
const options = {
searchText: '25',
searchItems: people,
exact: false
};
const results = search(options);
expect(results.length).toBe(1);
expect(results[0].age).toBe(25);
});

it('should include keys in the results if include is true', () => {
const options = {
searchText: 'John',
searchItems: people,
keys: ['name'],
include: true,
exact: false
};
const results = search(options);
expect(results.length).toBe(1);
expect(results[0]).toHaveProperty('name', 'John');
});

it('should not include keys in the results if include is false', () => {
const options = {
searchText: 'Jane',
searchItems: people,
keys: ['name'],
include: false,
exact: false
};
const results = search(options);
expect(results.length).toBe(0);
});
});
38 changes: 38 additions & 0 deletions src/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { SearchItem } from './types';
import { isKeyIncluded, addUniqueMatch } from './utils';

describe('isKeyIncluded', () => {
const keys = ['name', 'age'];

it('should return true when key is in the list and include is true', () => {
expect(isKeyIncluded('name', keys, true)).toBeTruthy();
});

it('should return false when key is not in the list and include is true', () => {
expect(isKeyIncluded('address', keys, true)).toBeFalsy();
});

it('should return true when key is not in the list and include is false', () => {
expect(isKeyIncluded('address', keys, false)).toBeTruthy();
});

it('should return false when key is in the list and include is false', () => {
expect(isKeyIncluded('name', keys, false)).toBeFalsy();
});
});

describe('addUniqueMatch', () => {
it('should add item to results if not already included', () => {
const results: SearchItem[] = [];
const item = { name: 'John', age: 30 };
addUniqueMatch(results, item);
expect(results).toContain(item);
});

it('should not add item to results if already included', () => {
const item = { name: 'Jane', age: 25 };
const results = [item];
addUniqueMatch(results, item);
expect(results).toEqual([item]);
});
});
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
"target": "ESNext"
},
"exclude": ["node_modules", "dist"],
"include": ["**/*.js", "**/*.ts"]
"include": ["*.cjs", "*.js", "**/*.ts"]
}
6 changes: 6 additions & 0 deletions tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"types": ["jest"]
}
}
Loading

0 comments on commit 96d2f68

Please sign in to comment.