Skip to content
This repository has been archived by the owner on Sep 13, 2024. It is now read-only.

Commit

Permalink
test: ✅ Added titlebar test using jest
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexTorresDev committed Jul 24, 2023
1 parent 60b0876 commit 04e89b8
Show file tree
Hide file tree
Showing 8 changed files with 2,042 additions and 7 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ jobs:
node-version: "lts/*"
cache: pnpm
- run: pnpm install --no-frozen-lockfile
- name: Test
run: pnpm test
- name: Build
run: pnpm build
- name: Test
run: pnpm test
- name: Upload artifact
uses: actions/upload-artifact@v3
with:
Expand Down
7 changes: 7 additions & 0 deletions __mocks__/base/common/color.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
Color: {
fromHex: jest.fn(),
isLighter: jest.fn(),
WHITE: 'mocked-white-color'
}
}
6 changes: 6 additions & 0 deletions __mocks__/base/common/platform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
platform: 'Windows',
isMacintosh: false,
isWindows: true,
isLinux: false,
}
10 changes: 10 additions & 0 deletions __mocks__/electron.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {
ipcRenderer: {
invoke: jest.fn(),
on: jest.fn(),
send: jest.fn()
},
Menu: {
buildFromTemplate: jest.fn()
},
}
55 changes: 55 additions & 0 deletions __test__/titlebar.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// customTitlebar.test.js

const { CustomTitlebar } = require('../dist/titlebar/index');
const { BOTTOM_TITLEBAR_HEIGHT } = require('../dist/consts');

jest.mock('electron');
jest.mock('base/common/color');
jest.mock('base/common/platform');

describe('CustomTitlebar', () => {
let titlebar;

beforeEach(() => {
jest.clearAllMocks();
titlebar = new CustomTitlebar();
});

afterEach(() => {
titlebar?.dispose();
});

test('should create an instance of CustomTitlebar', () => {
expect(titlebar).toBeInstanceOf(CustomTitlebar);
});

test('should update the title', () => {
titlebar.updateTitle('Test Title');
expect(titlebar.titleElement.innerText).toBe('Test Title');
expect(document.title).toBe('Test Title');
});

/* test('should update the background color', () => {
const newColor = 'mocked-new-color';
titlebar.updateBackground(newColor);
expect(titlebar.titlebarElement.style.backgroundColor).toBe(newColor);
}); */

test('should update the menu position', () => {
titlebar.updateMenuPosition('bottom');
expect(titlebar.titlebarElement.style.height).toBe(BOTTOM_TITLEBAR_HEIGHT);
expect(titlebar.containerElement.style.top).toBe(BOTTOM_TITLEBAR_HEIGHT);
expect(titlebar.menuBarContainer.classList.contains('bottom')).toBe(true);
});

/* test('should refresh the menu', async () => {
const mockedMenu = { items: [{ label: 'Test Menu' }] };
ipcRenderer.invoke.mockResolvedValue(mockedMenu);
await titlebar.refreshMenu();
expect(ipcRenderer.invoke).toHaveBeenCalledWith('request-application-menu');
expect(titlebar.menubarElement.push).toHaveBeenCalledWith(mockedMenu);
expect(titlebar.menubarElement.update).toHaveBeenCalled();
}); */
});
13 changes: 13 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = {
preset: 'ts-jest/presets/js-with-babel',
testEnvironment: 'jsdom',
moduleNameMapper: {
'^base/(.*)$': '<rootDir>/src/base/$1',
'^static/(.*)$': '<rootDir>/static/$1'
},
globals: {
'ts-jest': {
babelConfig: true,
}
},
};
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"dev": "npm run build && npm run start",
"build": "npm run clean && npm run build:package && npm run build:babel",
"postversion": "cp package.json .. && chmod u+x ../revert.sh && ../revert.sh",
"test": "echo \"Test script here\""
"test": "jest"
},
"author": "AlexTorresDev <[email protected]>",
"license": "MIT",
Expand All @@ -50,6 +50,7 @@
"devDependencies": {
"@babel/cli": "7.22.9",
"@babel/core": "7.22.9",
"@jest/globals": "29.6.1",
"@typescript-eslint/eslint-plugin": "6.1.0",
"@typescript-eslint/parser": "6.1.0",
"babel-plugin-import-require-as-string": "1.0.2",
Expand All @@ -62,8 +63,11 @@
"eslint-plugin-import": "2.27.5",
"eslint-plugin-n": "16.0.1",
"eslint-plugin-promise": "6.1.1",
"jest": "^29.6.1",
"jest-environment-jsdom": "29.6.1",
"rimraf": "5.0.1",
"standard": "17.1.0",
"ts-jest": "29.1.1",
"tsc-alias": "1.8.7",
"typescript": "5.1.6"
},
Expand Down
Loading

0 comments on commit 04e89b8

Please sign in to comment.