-
-
Notifications
You must be signed in to change notification settings - Fork 11.8k
Expand file tree
/
Copy pathapp.test.js
More file actions
138 lines (109 loc) · 4.53 KB
/
Copy pathapp.test.js
File metadata and controls
138 lines (109 loc) · 4.53 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import App from '../src/app';
import setupGhostApi from '../src/utils/api';
import {appRender} from './utils/test-utils';
import {site as FixtureSite, member as FixtureMember} from './utils/test-fixtures';
import i18n from '../src/utils/i18n';
import {vi} from 'vitest';
vi.mock('../src/utils/i18n', () => ({
default: {
changeLanguage: vi.fn(),
dir: vi.fn(),
t: vi.fn(str => str)
},
t: vi.fn(str => str)
}));
describe('App', function () {
beforeEach(function () {
// Stub window.location with a URL object so we have an expected origin
const location = new URL('http://example.com');
delete window.location;
window.location = location;
});
function setupApi({site = {}, member = {}} = {}) {
const defaultSite = FixtureSite.singleTier.basic;
const defaultMember = FixtureMember.free;
const siteFixtures = {
...defaultSite,
...site
};
const memberFixtures = {
...defaultMember,
...member
};
const ghostApi = setupGhostApi({siteUrl: 'http://example.com'});
ghostApi.init = vi.fn(() => {
return Promise.resolve({
site: siteFixtures,
member: memberFixtures
});
});
return ghostApi;
}
test('transforms portal links on render', async () => {
const link = document.createElement('a');
link.setAttribute('href', 'http://example.com/#/portal/signup');
document.body.appendChild(link);
const ghostApi = setupApi();
const utils = appRender(
<App siteUrl="http://example.com" api={ghostApi} />
);
await utils.findByTitle(/portal-popup/i);
expect(link.getAttribute('href')).toBe('#/portal/signup');
});
test('prefers locale prop over site locale for i18n language', async () => {
const ghostApi = setupApi({
site: {
locale: 'de'
}
});
const utils = appRender(
<App siteUrl="http://example.com" api={ghostApi} locale="en" />
);
await utils.findByTitle(/portal-popup/i);
i18n.changeLanguage.mock.calls.forEach((call) => {
expect(call[0]).toBe('en');
});
});
test('reloads page when popup closes with reloadOnPopupClose flag', () => {
const app = new App({siteUrl: 'http://example.com'});
window.location.reload = vi.fn();
app.state = {...app.state, showPopup: false, reloadOnPopupClose: true};
app.componentDidUpdate({}, {showPopup: true});
expect(window.location.reload).toHaveBeenCalledTimes(1);
});
test('does not reload when popup closes without reloadOnPopupClose flag', () => {
const app = new App({siteUrl: 'http://example.com'});
window.location.reload = vi.fn();
app.state = {...app.state, showPopup: false};
app.componentDidUpdate({}, {showPopup: true});
expect(window.location.reload).not.toHaveBeenCalled();
});
test('does not reload when reloadOnPopupClose is false', () => {
const app = new App({siteUrl: 'http://example.com'});
window.location.reload = vi.fn();
// Set reloadOnPopupClose to false explicitly and close the popup
app.state = {...app.state, showPopup: false, reloadOnPopupClose: false};
app.componentDidUpdate({}, {showPopup: true});
expect(window.location.reload).not.toHaveBeenCalled();
});
test('parses retention offer preview query data into account cancellation flow', () => {
const app = new App({siteUrl: 'http://example.com'});
const previewData = app.fetchOfferQueryStrData('redemption_type=retention&display_title=Before%2520you%2520go&display_description=Please%2520stay&type=percent&amount=100&duration=repeating&duration_in_months=2&cadence=month&tier_id=product_123&enabled=false');
expect(previewData.page).toBe('accountPlan');
expect(previewData.pageData).toMatchObject({
action: 'cancel'
});
expect(previewData.offers).toHaveLength(1);
expect(previewData.offers[0]).toMatchObject({
display_title: 'Before you go',
display_description: 'Please stay',
redemption_type: 'retention',
type: 'percent',
amount: 100,
duration: 'repeating',
duration_in_months: 2,
cadence: 'month'
});
expect(previewData.offers[0].tier).toMatchObject({id: 'product_123'});
});
});