-
Notifications
You must be signed in to change notification settings - Fork 2k
/
utils.js
193 lines (168 loc) · 6.83 KB
/
utils.js
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/**
* @jest-environment jsdom
*/
/**
* External dependencies
*/
import { assert } from 'chai';
/**
* Internal dependencies
*/
import * as PluginUtils from '../utils';
describe( 'Plugins Utils', () => {
describe( 'normalizePluginData', () => {
test( 'should have a method normalizePluginData', () => {
assert.isFunction( PluginUtils.normalizePluginData );
} );
test( 'normalizePluginData should normalise the plugin data', () => {
const plugin = { name: '<a href="http://google.com">google</a>' },
decodedPlugin = { name: '<a href="http://google.com">google</a>' };
assert.notEqual( decodedPlugin, PluginUtils.normalizePluginData( plugin ) );
} );
test( 'should add the author name to the plugins object', () => {
const plugin = JSON.parse( '{"author":"<a href=\\"http://jetpack.me\\">Automattic</a>"}' ),
normalizedPlugin = PluginUtils.normalizePluginData( plugin );
assert.equal( normalizedPlugin.author_name, 'Automattic' );
} );
test( 'should add the author url to the plugins object', () => {
const plugin = JSON.parse( '{"author":"<a href=\\"http://jetpack.me\\">Automattic</a>"}' ),
normalizedPlugin = PluginUtils.normalizePluginData( plugin );
assert.equal( normalizedPlugin.author_url, 'http://jetpack.me' );
} );
test( 'should get the best possible icon if available', () => {
const plugin = { icons: { '1x': '1x test icon', '2x': '2x test icon' } },
normalizedPlugin = PluginUtils.normalizePluginData( plugin );
assert.equal( normalizedPlugin.icon, '2x test icon' );
} );
} );
describe( 'getAllowedPluginData', () => {
test( 'should have a method getAllowedPluginData', () => {
assert.isFunction( PluginUtils.getAllowedPluginData );
} );
test( 'should stip out unknown keys', () => {
const plugin = { unknownKey: true };
assert.deepEqual( PluginUtils.getAllowedPluginData( plugin ), {} );
} );
test( 'should keep known keys', () => {
const plugin = {
active: false,
author: 'Automattic',
author_url: 'http://automattic.com',
autoupdate: false,
description: 'The first stop for every WordPress developer',
id: 'developer/developer',
name: 'Developer',
network: false,
plugin_url: 'http://wordpress.org/extend/plugins/developer/',
slug: 'developer',
version: '1.2.5',
update: {},
updating: false,
};
assert.deepEqual( PluginUtils.getAllowedPluginData( plugin ), plugin );
} );
} );
describe( 'extractAuthorName', () => {
test( 'should extract the author from the .org api response', () => {
const jetpackAuthorObj = JSON.parse(
'{"author":"<a href=\\"http://jetpack.me\\">Automattic</a>"}'
);
assert.equal( PluginUtils.extractAuthorName( jetpackAuthorObj.author ), 'Automattic' );
} );
test( 'should support names with special chars', () => {
const plugin = JSON.parse(
'{"author":"<a href=\\"http://test.com/\\">Ñáð</a>"}'
);
assert.equal( PluginUtils.extractAuthorName( plugin.author ), 'Ñáð' );
} );
} );
describe( 'extractScreenshots', () => {
test( 'should extract the screenshot data from the .org api response', () => {
const screenshotData = JSON.parse(
'{"screenshots":"<ul><li><a href=\\"http://jetpack.me\\"><img src=\\"http://url-toscreenshot.com\\" /></a><p>Caption!</p></li></ul>"}'
),
extractedScreenshotData = PluginUtils.extractScreenshots( screenshotData.screenshots );
assert.isArray( extractedScreenshotData );
assert.equal( extractedScreenshotData[ 0 ].url, 'http://url-toscreenshot.com/' );
assert.equal( extractedScreenshotData[ 0 ].caption, 'Caption!' );
} );
test( 'should extract the screenshot data from the .org api response with removed items', () => {
const screenshotData = JSON.parse(
'{"screenshots":"<ul><li><a href=\\"http://jetpack.me\\"><img /></a><p>Caption!</p></li><li><a href=\\"http://jetpack.me\\"><img src=\\"http://url-toscreenshot.com\\" /></a><p>Caption!</p></li></ul>"}'
),
extractedScreenshotData = PluginUtils.extractScreenshots( screenshotData.screenshots );
assert.isArray( extractedScreenshotData );
assert.equal( extractedScreenshotData[ 0 ].url, 'http://url-toscreenshot.com/' );
assert.equal( extractedScreenshotData[ 0 ].caption, 'Caption!' );
} );
test( 'should extract the screenshot data from the .org api response but if no screenshots urls then return null', () => {
const screenshotData = JSON.parse(
'{"screenshots":"<ul><li><a href=\\"http://jetpack.me\\"><img /></a><p>Caption!</p></li></ul>"}'
),
extractedScreenshotData = PluginUtils.extractScreenshots( screenshotData.screenshots );
assert.isNull( extractedScreenshotData );
} );
test( 'should return null if screenshots is empty string', () => {
const screenshotData = JSON.parse( '{"screenshots":""}' ),
extractedScreenshotData = PluginUtils.extractScreenshots( screenshotData.screenshots );
assert.isNull( extractedScreenshotData );
} );
test( 'should return null if we pass null instead of JSON”', () => {
const extractedScreenshotData = PluginUtils.extractScreenshots( null );
assert.isNull( extractedScreenshotData );
} );
} );
describe( 'normalizeCompatibilityList', () => {
test( 'should trunc the hotfix number if 0', () => {
const compatibility = { '1.5.0': {} };
assert.equal( PluginUtils.normalizeCompatibilityList( compatibility )[ 0 ], '1.5' );
} );
test( 'should not trunc the minor number if 0', () => {
const compatibility = { '1.0': {} };
assert.equal( PluginUtils.normalizeCompatibilityList( compatibility )[ 0 ], '1.0' );
} );
test( 'should provide an ordered compatibility list', () => {
const compatibility = {
'1.1.3': {},
'01.1.4': {},
10.3: {},
1.5: {},
},
orderedCompatibility = PluginUtils.normalizeCompatibilityList( compatibility );
assert.equal( orderedCompatibility.length, 4 );
assert.deepEqual( orderedCompatibility, [ '1.1.3', '1.1.4', '1.5', '10.3' ] );
} );
} );
describe( 'filterNotices', () => {
let logs;
beforeEach( () => {
logs = [
{
status: 'status',
action: 'action',
site: { ID: '123' },
plugin: { slug: 'jetpack' },
},
{
status: 'status',
action: 'action',
site: { ID: '321' },
plugin: {},
},
];
} );
test( 'should return a list of notices that match the site', () => {
logs[ 0 ].plugin = {};
const log = PluginUtils.filterNotices( logs, { ID: '123' }, null );
assert.deepEqual( log, [ logs[ 0 ] ] );
} );
test( 'should return a list of notices that match a plugin', () => {
const log = PluginUtils.filterNotices( logs, null, 'jetpack' );
assert.deepEqual( log, [ logs[ 0 ] ] );
} );
test( 'should return a list of notices that match the site and plugin', () => {
const log = PluginUtils.filterNotices( logs, { ID: '123' }, 'jetpack' );
assert.deepEqual( log, [ logs[ 0 ] ] );
} );
} );
} );