-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPageAPI.test.ts
More file actions
81 lines (69 loc) · 2.65 KB
/
PageAPI.test.ts
File metadata and controls
81 lines (69 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import * as chai from 'chai';
import * as sinon from 'sinon';
import sinonChai from 'sinon-chai';
import type { PageManager, Route } from '../../index.ts';
import { ProxyPageManager } from '../../index.ts';
import type { PageAPIInstance } from './PageAPI.ts';
import { PageAPI } from './PageAPI.ts';
chai.use(sinonChai);
const { expect } = chai;
afterEach(() => {
sinon.restore();
PageAPI.resetInstance();
});
describe('PageAPI', () => {
let pageAPI: PageAPIInstance;
let mockRoute: Route;
beforeEach(() => {
pageAPI = PageAPI.getInstance();
mockRoute = { path: '/products/:id', url: '/products/123' };
});
it('should return the same instance on multiple calls', () => {
const pageAPIInstance1 = PageAPI.getInstance();
const pageAPIInstance2 = PageAPI.getInstance();
expect(pageAPIInstance1).to.equal(pageAPIInstance2);
});
it('should return an instance of ProxyPageManager for getPageManager', () => {
const pageManager = pageAPI.getPageManager();
expect(pageManager).to.be.instanceOf(ProxyPageManager);
});
it('should set and get the global page manager', () => {
const mockPageManager: PageManager = {
setCurrentRoute: sinon.stub(),
getCurrentRoute: sinon.stub().returns(mockRoute),
getCurrentPageId: sinon.stub().returns('test-page-id'),
clearCurrentRoute: sinon.stub(),
setPageLabel: sinon.stub(),
getPageLabel: sinon.stub(),
};
pageAPI.setGlobalPageManager(mockPageManager);
const pageManager = pageAPI.getPageManager();
expect(pageManager).to.be.instanceOf(ProxyPageManager);
expect((pageManager as ProxyPageManager).getDelegate()).to.equal(
mockPageManager,
);
});
it('should forward calls to the page manager', () => {
const mockPageManager: PageManager = {
setCurrentRoute: sinon.stub(),
getCurrentRoute: sinon.stub().returns(mockRoute),
getCurrentPageId: sinon.stub().returns('test-page-id'),
clearCurrentRoute: sinon.stub(),
setPageLabel: sinon.stub(),
getPageLabel: sinon.stub(),
};
pageAPI.setGlobalPageManager(mockPageManager);
pageAPI.setCurrentRoute(mockRoute);
expect(mockPageManager.setCurrentRoute).to.have.been.calledOnceWith(
mockRoute,
);
const route = pageAPI.getCurrentRoute();
void expect(route).to.equal(mockRoute);
void expect(mockPageManager.getCurrentRoute).to.have.been.calledOnce;
const pageId = pageAPI.getCurrentPageId();
expect(pageId).to.equal('test-page-id');
void expect(mockPageManager.getCurrentPageId).to.have.been.calledOnce;
pageAPI.clearCurrentRoute();
void expect(mockPageManager.clearCurrentRoute).to.have.been.calledOnce;
});
});