Skip to content

Commit 04ffa74

Browse files
committed
Move tests
1 parent 46c70eb commit 04ffa74

File tree

3 files changed

+91
-93
lines changed

3 files changed

+91
-93
lines changed

test/common/gristUrls.ts

Lines changed: 1 addition & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {
2-
decodeUrl, commonUrls as defaultCommonUrls, getCommonUrls,
3-
getHostType, getSlugIfNeeded, IGristUrlState, parseFirstUrlPart
2+
decodeUrl, getHostType, getSlugIfNeeded, IGristUrlState, parseFirstUrlPart
43
} from 'app/common/gristUrls';
54
import {assert} from 'chai';
65
import Sinon from 'sinon';
@@ -166,94 +165,4 @@ describe('gristUrls', function() {
166165
assert.strictEqual(getSlugIfNeeded({id, urlId, name: "S&P500 is ~$4,894.16"}), 'SandP500-is-dollar489416');
167166
});
168167
});
169-
170-
describe('getCommonUrls', function () {
171-
it('should return the default URLs', function () {
172-
const commonUrls = getCommonUrls();
173-
assert.isObject(commonUrls);
174-
assert.equal(commonUrls.help, "https://support.getgrist.com");
175-
});
176-
177-
describe("with GRIST_CUSTOM_COMMON_URLS env var set", function () {
178-
it('should return the values set by the GRIST_CUSTOM_COMMON_URLS env var', function () {
179-
const customHelpCenterUrl = "http://custom.helpcenter";
180-
sandbox.define(process.env, 'GRIST_CUSTOM_COMMON_URLS',
181-
`{"help": "${customHelpCenterUrl}"}`);
182-
const commonUrls = getCommonUrls();
183-
assert.isObject(commonUrls);
184-
assert.equal(commonUrls.help, customHelpCenterUrl);
185-
assert.equal(commonUrls.helpAccessRules, "https://support.getgrist.com/access-rules");
186-
});
187-
188-
it('should throw when keys extraneous to the ICommonUrls interface are added', function () {
189-
const nonExistingKey = 'iDontExist';
190-
sandbox.define(process.env, 'GRIST_CUSTOM_COMMON_URLS',
191-
`{"${nonExistingKey}": "foo", "help": "https://getgrist.com"}`);
192-
assert.throws(() => getCommonUrls(), `value.${nonExistingKey} is extraneous`);
193-
});
194-
195-
it('should throw when the passed JSON is malformed', function () {
196-
sandbox.define(process.env, 'GRIST_CUSTOM_COMMON_URLS', '{"malformed": 42');
197-
assert.throws(() => getCommonUrls(), 'The JSON passed to GRIST_CUSTOM_COMMON_URLS is malformed');
198-
});
199-
200-
it('should throw when keys has unexpected type', function () {
201-
const regularValueKey = 'help';
202-
const numberValueKey = 'helpAccessRules';
203-
const objectValueKey = 'helpAssistant';
204-
const arrayValueKey = 'helpAssistantDataUse';
205-
const nullValueKey = 'helpFormulaAssistantDataUse';
206-
207-
sandbox.define(process.env, 'GRIST_CUSTOM_COMMON_URLS',
208-
JSON.stringify({
209-
[regularValueKey]: "https://getgrist.com",
210-
[numberValueKey]: 42,
211-
[objectValueKey]: {"key": "value"},
212-
[arrayValueKey]: ["foo"],
213-
})
214-
);
215-
const buildExpectedErrRegEx = (...keys: string[]) => new RegExp(
216-
keys.map(key => `value\\.${key}`).join('.*'),
217-
'ms'
218-
);
219-
assert.throws(() => getCommonUrls(), buildExpectedErrRegEx(numberValueKey, objectValueKey, arrayValueKey));
220-
sandbox.restore();
221-
sandbox.define(process.env, 'GRIST_CUSTOM_COMMON_URLS',
222-
JSON.stringify({
223-
[regularValueKey]: "https://getgrist.com",
224-
[nullValueKey]: null,
225-
})
226-
);
227-
assert.throws(() => getCommonUrls(), buildExpectedErrRegEx(nullValueKey));
228-
});
229-
230-
it("should return the default URLs when the parsed value is not an object", function () {
231-
sandbox.define(process.env, "GRIST_CUSTOM_COMMON_URLS", "42");
232-
assert.deepEqual(getCommonUrls(), defaultCommonUrls);
233-
sandbox.restore();
234-
sandbox.define(process.env, "GRIST_CUSTOM_COMMON_URLS", "null");
235-
assert.deepEqual(getCommonUrls(), defaultCommonUrls);
236-
});
237-
});
238-
239-
describe("client-side when customized by the admin", function () {
240-
it("should read the admin-defined values gristConfig", function () {
241-
sandbox.define(globalThis, 'window', {
242-
gristConfig: {
243-
adminDefinedUrls: JSON.stringify({
244-
help: "https://getgrist.com"
245-
})
246-
},
247-
// Fake location to make isClient() believe the code is executed client-side.
248-
location: {
249-
hostname: 'getgrist.com'
250-
},
251-
});
252-
const commonUrls = getCommonUrls();
253-
assert.isObject(commonUrls);
254-
assert.equal(commonUrls.help, "https://getgrist.com");
255-
assert.equal(commonUrls.helpAccessRules, "https://support.getgrist.com/access-rules");
256-
});
257-
});
258-
});
259168
});

test/fixtures/projects/DocMenu.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const globalWindow = {
1414
timestampMs : 0,
1515
pluginUrl : 'http://localhost:0',
1616
plugins: []
17-
}
17+
} as any
1818
};
1919
const mockAppModel = TopAppModelImpl.create(null, globalWindow, mockUserApi);
2020

test/server/lib/commonUrls.ts

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import {
2+
commonUrls as defaultCommonUrls,
3+
getCommonUrls
4+
} from 'app/server/lib/commonUrls';
5+
6+
import { assert } from 'chai';
7+
import Sinon from 'sinon';
8+
9+
describe('commonUrls', function () {
10+
let sandbox: Sinon.SinonSandbox;
11+
12+
beforeEach(function () {
13+
sandbox = Sinon.createSandbox();
14+
});
15+
16+
afterEach(function () {
17+
sandbox.restore();
18+
});
19+
20+
describe('getCommonUrls', function () {
21+
it('should return the default URLs', function () {
22+
const commonUrls = getCommonUrls();
23+
assert.isObject(commonUrls);
24+
assert.equal(commonUrls.help, "https://support.getgrist.com");
25+
});
26+
27+
describe("with GRIST_CUSTOM_COMMON_URLS env var set", function () {
28+
it('should return the values set by the GRIST_CUSTOM_COMMON_URLS env var', function () {
29+
const customHelpCenterUrl = "http://custom.helpcenter";
30+
sandbox.define(process.env, 'GRIST_CUSTOM_COMMON_URLS',
31+
`{"help": "${customHelpCenterUrl}"}`);
32+
const commonUrls = getCommonUrls();
33+
assert.isObject(commonUrls);
34+
assert.equal(commonUrls.help, customHelpCenterUrl);
35+
assert.equal(commonUrls.helpAccessRules, "https://support.getgrist.com/access-rules");
36+
});
37+
38+
it('should throw when keys extraneous to the ICommonUrls interface are added', function () {
39+
const nonExistingKey = 'iDontExist';
40+
sandbox.define(process.env, 'GRIST_CUSTOM_COMMON_URLS',
41+
`{"${nonExistingKey}": "foo", "help": "https://getgrist.com"}`);
42+
assert.throws(() => getCommonUrls(), `value.${nonExistingKey} is extraneous`);
43+
});
44+
45+
it('should throw when the passed JSON is malformed', function () {
46+
sandbox.define(process.env, 'GRIST_CUSTOM_COMMON_URLS', '{"malformed": 42');
47+
assert.throws(() => getCommonUrls(), 'The JSON passed to GRIST_CUSTOM_COMMON_URLS is malformed');
48+
});
49+
50+
it('should throw when keys has unexpected type', function () {
51+
const regularValueKey = 'help';
52+
const numberValueKey = 'helpAccessRules';
53+
const objectValueKey = 'helpAssistant';
54+
const arrayValueKey = 'helpAssistantDataUse';
55+
const nullValueKey = 'helpFormulaAssistantDataUse';
56+
57+
sandbox.define(process.env, 'GRIST_CUSTOM_COMMON_URLS',
58+
JSON.stringify({
59+
[regularValueKey]: "https://getgrist.com",
60+
[numberValueKey]: 42,
61+
[objectValueKey]: {"key": "value"},
62+
[arrayValueKey]: ["foo"],
63+
})
64+
);
65+
const buildExpectedErrRegEx = (...keys: string[]) => new RegExp(
66+
keys.map(key => `value\\.${key}`).join('.*'),
67+
'ms'
68+
);
69+
assert.throws(() => getCommonUrls(), buildExpectedErrRegEx(numberValueKey, objectValueKey, arrayValueKey));
70+
sandbox.restore();
71+
sandbox.define(process.env, 'GRIST_CUSTOM_COMMON_URLS',
72+
JSON.stringify({
73+
[regularValueKey]: "https://getgrist.com",
74+
[nullValueKey]: null,
75+
})
76+
);
77+
assert.throws(() => getCommonUrls(), buildExpectedErrRegEx(nullValueKey));
78+
});
79+
80+
it("should return the default URLs when the parsed value is not an object", function () {
81+
sandbox.define(process.env, "GRIST_CUSTOM_COMMON_URLS", "42");
82+
assert.deepEqual(getCommonUrls(), defaultCommonUrls);
83+
sandbox.restore();
84+
sandbox.define(process.env, "GRIST_CUSTOM_COMMON_URLS", "null");
85+
assert.deepEqual(getCommonUrls(), defaultCommonUrls);
86+
});
87+
});
88+
});
89+
});

0 commit comments

Comments
 (0)