Skip to content

Commit

Permalink
refactor(testing): add jest in context of primefaces#15009
Browse files Browse the repository at this point in the history
NOTE:
1. Configured jest according to https://thymikee.github.io/jest-preset-angular/docs/getting-started/installation
2. In was recommended in diverse sources to comment out the files
in `tsconfig.spec.json` to avoid conflicts with jest
  • Loading branch information
juri-sinitson committed Dec 20, 2024
1 parent f00d74b commit 6b99b33
Show file tree
Hide file tree
Showing 8 changed files with 1,322 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
"[typescript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true
}
},
"angular.enable-strict-mode-prompt": false
}
35 changes: 35 additions & 0 deletions basic-test.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* This is a basic test file is to test
* if your current test tooling works in general.
*/
import { Component } from '@angular/core';
import { TestBed } from '@angular/core/testing';

interface MyInterface {
name: string;
}

@Component({
template: ``
})
class DummyComponent {}

function p1(): MyInterface {
return { name: 'p1' };
}

describe('test', () => {
it('p1', () => {
expect(p1().name).toBe('p1');
});

it('should create dummy component', () => {
TestBed.configureTestingModule({
declarations: [DummyComponent]
}).compileComponents();

const fixture = TestBed.createComponent(DummyComponent);
const component = fixture.componentInstance;
expect(component).toBeTruthy();
});
});
13 changes: 13 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { Config } from 'jest';

const config: Config = {
collectCoverage: true,
coverageReporters: ['html'],
preset: 'jest-preset-angular',
moduleNameMapper: {
'^primeng/(.*)': '<rootDir>/packages/primeng/src/$1/public_api'
},
setupFilesAfterEnv: ['<rootDir>/setup-jest.ts']
};

export default config;
11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
"format:check": "prettier --check \"**/*.{js,mjs,ts,mts,d.ts,html}\"",
"lint": "eslint --ext \".js,.mjs,.ts,.mts\" --ignore-path .gitignore . --cache",
"lint:fix": "eslint --fix --ext \".js,.mjs,.ts,.mts\" --ignore-path .gitignore .",
"test:unit": "pnpm --filter primeng test:unit"
"test:unit": "pnpm --filter primeng test:unit",
"test:jest": "jest",
"test:confirm-jest": "jest basic-test.spec.ts"
},
"devDependencies": {
"@angular-devkit/build-angular": "catalog:angular19",
Expand Down Expand Up @@ -61,10 +63,13 @@
"fs-extra": "^11.2.0",
"glob": "^10.4.2",
"husky": "^9.1.6",
"jest": "^29.7.0",
"jest-preset-angular": "^14.3.1",
"lint-staged": "^12.0.0",
"ng-packagr": "catalog:angular19",
"pnpm": "^9.6.0",
"prettier": "^3.0.0",
"ts-jest": "^29.2.5",
"tsup": "^8.1.0",
"typescript": "catalog:angular19"
},
Expand All @@ -73,6 +78,8 @@
},
"packageManager": "[email protected]",
"lint-staged": {
"**/*.{js,mjs,ts,mts,d.ts,html}": ["prettier --write"]
"**/*.{js,mjs,ts,mts,d.ts,html}": [
"prettier --write"
]
}
}
34 changes: 34 additions & 0 deletions packages/primeng/src/table/table.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { TestBed } from '@angular/core/testing';

import { FiltersArg } from '../api/fitlersarg';
import { TableService } from './table.service';

describe('TableService', () => {
describe('filter entries', () => {
describe('by single field aka locally', () => {
let service: TableService;

beforeEach(() => {
// DEBUG! Prove this in the official docs
TestBed.configureTestingModule({
providers: [TableService]
});
service = TestBed.inject(TableService);
});

it('should filter by starts with', () => {
const data = [{ name: 'foo' }, { name: 'bar' }];
const filters: FiltersArg = {
name: {
value: 'f',
matchMode: 'startsWith'
}
};

const result = service.filter(data, filters, [], 'startsWith');
expect(result).toEqual([{ name: 'foo' }]);
});
});
describe('by all fields aka globally', () => {});
});
});
Loading

0 comments on commit 6b99b33

Please sign in to comment.