UNPKG

4.46 kBPlain TextView Raw
1// *****************************************************************************
2// Copyright (C) 2021 Ericsson and others.
3//
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License v. 2.0 which is available at
6// http://www.eclipse.org/legal/epl-2.0.
7//
8// This Source Code may also be made available under the following Secondary
9// Licenses when the conditions for such availability set forth in the Eclipse
10// Public License v. 2.0 are satisfied: GNU General Public License, version 2
11// with the GNU Classpath Exception which is available at
12// https://www.gnu.org/software/classpath/license.html.
13//
14// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15// *****************************************************************************
16
17import { enableJSDOM } from '../test/jsdom';
18
19let disableJSDOM = enableJSDOM();
20
21import * as assert from 'assert';
22import { Container } from 'inversify';
23import { bindPreferenceService } from '../frontend-application-bindings';
24import { PreferenceSchemaProperties, PreferenceSchemaProvider } from './preference-contribution';
25import { FrontendApplicationConfigProvider } from '../frontend-application-config-provider';
26
27disableJSDOM();
28
29process.on('unhandledRejection', (reason, promise) => {
30 console.error(reason);
31 throw reason;
32});
33
34// const { expect } = require('chai');
35let testContainer: Container;
36
37function createTestContainer(): Container {
38 const result = new Container();
39 bindPreferenceService(result.bind.bind(result));
40 return result;
41}
42
43const EDITOR_FONT_SIZE_PROPERTIES: PreferenceSchemaProperties = {
44 'editor.fontSize': {
45 type: 'number',
46 default: 14,
47 overridable: true
48 },
49};
50const EDITOR_INSERT_SPACES_PROPERTIES: PreferenceSchemaProperties = {
51 'editor.insertSpaces': {
52 type: 'boolean',
53 default: true,
54 overridable: true
55 },
56};
57
58describe('Preference Schema Provider', () => {
59 let prefSchema: PreferenceSchemaProvider;
60
61 before(() => {
62 disableJSDOM = enableJSDOM();
63 FrontendApplicationConfigProvider.set({
64 preferences: {
65 'editor.fontSize': 20,
66 '[typescript]': { 'editor.fontSize': 24 }
67 }
68 });
69 });
70
71 after(() => {
72 disableJSDOM();
73 });
74
75 beforeEach(async () => {
76 testContainer = createTestContainer();
77 prefSchema = testContainer.get(PreferenceSchemaProvider);
78 });
79
80 it('Should load all preferences specified in the frontend config.', () => {
81 assert.strictEqual(prefSchema.get('editor.fontSize'), 20);
82 assert.strictEqual(prefSchema.get('[typescript].editor.fontSize'), 24);
83 });
84
85 it('Should favor the default specified in the package.json over a default registered by a schema', () => {
86 prefSchema.setSchema({
87 properties: {
88 ...EDITOR_FONT_SIZE_PROPERTIES
89 }
90 });
91
92 assert.strictEqual(prefSchema.get('editor.fontSize'), 20);
93 });
94
95 it('Should merge language-specific overrides from schemas and the package.json', () => {
96 prefSchema.setSchema({
97 properties: {
98 ...EDITOR_FONT_SIZE_PROPERTIES,
99 ...EDITOR_INSERT_SPACES_PROPERTIES,
100 '[typescript]': {
101 type: 'object',
102 default: {
103 'editor.insertSpaces': false
104 }
105 }
106 }
107 });
108 assert.strictEqual(prefSchema.get('editor.insertSpaces'), true);
109 assert.strictEqual(prefSchema.get('[typescript].editor.insertSpaces'), false);
110 assert.strictEqual(prefSchema.get('[typescript].editor.fontSize'), 24);
111 });
112
113 it('Should favor package.json specifications in the merge process', () => {
114 prefSchema.setSchema({
115 properties: {
116 ...EDITOR_FONT_SIZE_PROPERTIES,
117 ...EDITOR_INSERT_SPACES_PROPERTIES,
118 '[typescript]': {
119 type: 'object',
120 default: {
121 'editor.insertSpaces': false,
122 'editor.fontSize': 36,
123 }
124 }
125 }
126 });
127 assert.strictEqual(prefSchema.get('[typescript].editor.insertSpaces'), false);
128 assert.strictEqual(prefSchema.get('[typescript].editor.fontSize'), 24);
129 });
130});