Skip to content

Commit

Permalink
added remaining tests. main.ts and preload.ts still missing. moved mo…
Browse files Browse the repository at this point in the history
…cks folder.
  • Loading branch information
dkrohmer committed Jul 17, 2024
1 parent ed9dad1 commit 081bfa2
Show file tree
Hide file tree
Showing 16 changed files with 718 additions and 43 deletions.
3 changes: 2 additions & 1 deletion .erb/configs/webpack.config.base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ const configuration: webpack.Configuration = {
rules: [
{
test: /\.[jt]sx?$/,
exclude: /node_modules/,
// exclude: /node_modules/,
exclude: /node_modules|__mocks__/,
use: {
loader: 'ts-loader',
options: {
Expand Down
5 changes: 5 additions & 0 deletions mocks/app-root-path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const path = require('path');

module.exports = {
path: path.resolve(__dirname, '../../')

Check failure on line 4 in mocks/app-root-path.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

Insert `,`

Check failure on line 4 in mocks/app-root-path.ts

View workflow job for this annotation

GitHub Actions / test (macos-latest)

Insert `,`

Check failure on line 4 in mocks/app-root-path.ts

View workflow job for this annotation

GitHub Actions / test (macos-latest)

Insert `,`

Check failure on line 4 in mocks/app-root-path.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

Insert `,`
};
File renamed without changes.
20 changes: 20 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@
"moduleNameMapper": {
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/.erb/mocks/fileMock.js",
"\\.(css|less|sass|scss)$": "identity-obj-proxy",
"^electron$": "<rootDir>/src/__mocks__/electron-mock.ts"

"^electron$": "<rootDir>/mocks/electron-mock.ts",
"app-root-path": "<rootDir>/mocks/app-root-path.ts"
},
"setupFiles": [
"./.erb/scripts/check-build-exists.ts"
Expand Down Expand Up @@ -148,6 +148,7 @@
"@types/electron": "^1.6.10",
"@types/jest": "^29.5.12",
"@types/js-cookie": "^3.0.6",
"@types/mock-fs": "^4.13.4",
"@types/node": "20.6.2",
"@types/react": "^18.2.21",
"@types/react-beforeunload": "^2.1.5",
Expand Down Expand Up @@ -189,6 +190,7 @@
"jest": "^29.7.0",
"jest-environment-jsdom": "^29.7.0",
"mini-css-extract-plugin": "^2.7.6",
"mock-fs": "^5.2.0",
"prettier": "^3.0.3",
"react-refresh": "^0.14.0",
"react-test-renderer": "^18.2.0",
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/main/controllers/IncrementController.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ipcMain, ipcRenderer } from '../../../__mocks__/electron-mock';
import { ipcMain, ipcRenderer } from 'electron';
import { IncrementService } from '../../../main/services/IncrementService';
import { IncrementController } from '../../../main/controllers/IncrementController';
import { Increment } from '../../../main/models/Increment';
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/main/controllers/ModelController.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ipcMain, ipcRenderer } from '../../../__mocks__/electron-mock';
import { ipcMain, ipcRenderer } from 'electron';
import { ModelService } from '../../../main/services/ModelService';
import { ModelController } from '../../../main/controllers/ModelController';
import { Model } from '../../../main/models/Model';
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/main/controllers/ProductController.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ipcMain, ipcRenderer } from '../../../__mocks__/electron-mock';
import { ipcMain, ipcRenderer } from 'electron';
import { ProductService } from '../../../main/services/ProductService';
import { ProductController } from '../../../main/controllers/ProductController';
import { Product } from '../../../main/models/Product';
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/main/controllers/VersionController.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ipcMain, ipcRenderer } from '../../../__mocks__/electron-mock';
import { ipcMain, ipcRenderer } from 'electron';
import { VersionService } from '../../../main/services/VersionService';
import { VersionController } from '../../../main/controllers/VersionController';
import { Version } from '../../../main/models/Version';
Expand Down
184 changes: 184 additions & 0 deletions src/__tests__/main/helpers/entityBuilder.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
// __tests__/entityBuilder.test.ts
import { buildProductEntity } from '../../../main/helpers/entityBuilder';
import { Product } from '../../../main/models/Product';
import { Responsible } from '../../../main/models/Responsible';
import { Increment } from '../../../main/models/Increment';
import { Model } from '../../../main/models/Model';
import { Version } from '../../../main/models/Version';

// Mock the model classes to avoid importing TypeORM and database dependencies
jest.mock('../../../main/models/Product');
jest.mock('../../../main/models/Responsible');
jest.mock('../../../main/models/Increment');
jest.mock('../../../main/models/Model');
jest.mock('../../../main/models/Version');

describe('buildProductEntity', () => {
beforeEach(() => {
jest.clearAllMocks();
});

it('should build a product entity correctly with all fields', () => {
const productData = new Product();
productData.name = 'Test Product';
productData.description = 'This is a test product';
productData.startsAt = new Date('2024-01-01T00:00:00.000Z');
productData.endsAt = new Date('2024-12-31T23:59:59.999Z');
productData.increments = [
{
name: 'Increment 1',
incrementIndex: 0,
models: [
{
name: 'Model 1',
versions: [
{ payload: '{}', thumbnail: '', versionIndex: 0 },
],
},
],
} as Increment,
];
productData.responsibles = [
{ firstName: 'John', lastName: 'Doe', role: 'Manager' } as Responsible,
];

const product = buildProductEntity(productData);

expect(product.name).toBe('Test Product');
expect(product.description).toBe('This is a test product');
expect(product.startsAt).toEqual(new Date('2024-01-01T00:00:00.000Z'));
expect(product.endsAt).toEqual(new Date('2024-12-31T23:59:59.999Z'));
expect(product.increments).toHaveLength(1);
expect(product.responsibles).toHaveLength(1);
});

it('should throw an error if the end date is earlier than the start date', () => {
const productData = new Product();
productData.name = 'Test Product';
productData.startsAt = new Date('2024-01-01T00:00:00.000Z');
productData.endsAt = new Date('2023-12-31T23:59:59.999Z');

expect(() => buildProductEntity(productData)).toThrow(
'The end date cannot be earlier than the start date.'
);
});

it('should handle missing optional fields correctly', () => {
const productData = new Product();
productData.name = 'Test Product';
productData.startsAt = new Date('2024-01-01T00:00:00.000Z');

const product = buildProductEntity(productData);

expect(product.name).toBe('Test Product');
expect(product.description).toBeNull();
expect(product.startsAt).toEqual(new Date('2024-01-01T00:00:00.000Z'));
expect(product.endsAt).toBeNull();
expect(product.increments).toHaveLength(1);
expect(product.increments[0].name).toBe('Test Product - Baseline');
expect(product.responsibles).toBeUndefined();
});

it('should handle startsAt as null correctly', () => {
const productData = new Product();
productData.name = 'Test Product';
productData.startsAt = null;

const product = buildProductEntity(productData);

expect(product.startsAt).toBeNull();
});

it('should create a default increment if no increments are provided', () => {
const productData = new Product();
productData.name = 'Test Product';
productData.startsAt = new Date('2024-01-01T00:00:00.000Z');
productData.responsibles = [
{ firstName: 'John', lastName: 'Doe', role: 'Manager' } as Responsible,
];

const product = buildProductEntity(productData);

expect(product.increments).toHaveLength(1);
expect(product.increments[0].name).toBe('Test Product - Baseline');
});

it('should handle multiple increments and responsibles correctly', () => {
const productData = new Product();
productData.name = 'Test Product';
productData.startsAt = new Date('2024-01-01T00:00:00.000Z');
productData.increments = [
{ name: 'Increment 1', incrementIndex: 0 } as Increment,
{ name: 'Increment 2', incrementIndex: 1 } as Increment,
];
productData.responsibles = [
{ firstName: 'John', lastName: 'Doe', role: 'Manager' } as Responsible,
{ firstName: 'Jane', lastName: 'Smith', role: 'Engineer' } as Responsible,
];

const product = buildProductEntity(productData);

expect(product.increments).toHaveLength(2);
expect(product.responsibles).toHaveLength(2);
});

it('should create a default version if no versions are provided in the model', () => {
const productData = new Product();
productData.name = 'Test Product';
productData.startsAt = new Date('2024-01-01T00:00:00.000Z');
productData.increments = [
{
name: 'Increment 1',
incrementIndex: 0,
models: [
{
name: 'Model 1',
} as Model,
],
} as Increment,
];

const product = buildProductEntity(productData);

expect(product.increments[0].models[0].versions).toHaveLength(1);
expect(product.increments[0].models[0].versions[0].payload).toBe('{}');
expect(product.increments[0].models[0].versions[0].thumbnail).toBe('');
});

it('should set the versionIndex correctly if no versionIndex is provided', () => {
const productData = new Product();
productData.name = 'Test Product';
productData.startsAt = new Date('2024-01-01T00:00:00.000Z');
productData.increments = [
{
name: 'Increment 1',
incrementIndex: 0,
models: [
{
name: 'Model 1',
versions: [
{ payload: '{}', thumbnail: '' },
] as Version[],
} as Model,
],
} as Increment,
];

const product = buildProductEntity(productData);

expect(product.increments[0].models[0].versions[0].versionIndex).toBe(0);
});

it('should handle responsible role as null correctly', () => {
const productData = new Product();
productData.name = 'Test Product';
productData.startsAt = new Date('2024-01-01T00:00:00.000Z');
productData.responsibles = [
{ firstName: 'John', lastName: 'Doe' } as Responsible,
];

const product = buildProductEntity(productData);

expect(product.responsibles[0].role).toBeNull();
});
});
Loading

0 comments on commit 081bfa2

Please sign in to comment.