feat: extend navigationHistory API (#42014)

* feat: extend navigationHistory API

* refactor: simplify index checking

* refactor: rename 'getHistory' and 'replaceHistory' methods of navigationHistory

* refactor: rename delete*() methods to remove*()

* feat: remove navigationHistory.replaceHistory()

* tests: add tests for removeEntryAtIndex and getAllEntries
This commit is contained in:
Vít Černý 2024-08-19 21:46:04 +02:00 committed by GitHub
parent 4c3014944c
commit 189675575c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 105 additions and 6 deletions

View file

@ -567,6 +567,39 @@ describe('webContents module', () => {
w = new BrowserWindow({ show: false });
});
afterEach(closeAllWindows);
describe('navigationHistory.removeEntryAtIndex(index) API', () => {
it('should remove a navigation entry given a valid index', async () => {
await w.loadURL(urlPage1);
await w.loadURL(urlPage2);
await w.loadURL(urlPage3);
const initialLength = w.webContents.navigationHistory.length();
const wasRemoved = w.webContents.navigationHistory.removeEntryAtIndex(1); // Attempt to remove the second entry
const newLength = w.webContents.navigationHistory.length();
expect(wasRemoved).to.be.true();
expect(newLength).to.equal(initialLength - 1);
});
it('should not remove the current active navigation entry', async () => {
await w.loadURL(urlPage1);
await w.loadURL(urlPage2);
const activeIndex = w.webContents.navigationHistory.getActiveIndex();
const wasRemoved = w.webContents.navigationHistory.removeEntryAtIndex(activeIndex);
expect(wasRemoved).to.be.false();
});
it('should return false given an invalid index larger than history length', async () => {
await w.loadURL(urlPage1);
const wasRemoved = w.webContents.navigationHistory.removeEntryAtIndex(5); // Index larger than history length
expect(wasRemoved).to.be.false();
});
it('should return false given an invalid negative index', async () => {
await w.loadURL(urlPage1);
const wasRemoved = w.webContents.navigationHistory.removeEntryAtIndex(-1); // Negative index
expect(wasRemoved).to.be.false();
});
});
describe('navigationHistory.canGoBack and navigationHistory.goBack API', () => {
it('should not be able to go back if history is empty', async () => {
expect(w.webContents.navigationHistory.canGoBack()).to.be.false();
@ -706,6 +739,24 @@ describe('webContents module', () => {
expect(w.webContents.navigationHistory.length()).to.equal(1);
});
});
describe('navigationHistory.getAllEntries() API', () => {
it('should return all navigation entries as an array of NavigationEntry objects', async () => {
await w.loadURL(urlPage1);
await w.loadURL(urlPage2);
await w.loadURL(urlPage3);
const entries = w.webContents.navigationHistory.getAllEntries();
expect(entries.length).to.equal(3);
expect(entries[0]).to.deep.equal({ url: urlPage1, title: 'Page 1' });
expect(entries[1]).to.deep.equal({ url: urlPage2, title: 'Page 2' });
expect(entries[2]).to.deep.equal({ url: urlPage3, title: 'Page 3' });
});
it('should return an empty array when there is no navigation history', async () => {
const entries = w.webContents.navigationHistory.getAllEntries();
expect(entries.length).to.equal(0);
});
});
});
describe('getFocusedWebContents() API', () => {