-
-
Notifications
You must be signed in to change notification settings - Fork 8k
/
user.test.ts
165 lines (150 loc) · 5.46 KB
/
user.test.ts
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
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import fs from 'fs-extra';
import path from 'path';
import _ from 'lodash';
import imageSize from 'image-size';
import {Joi} from '@docusaurus/utils-validation';
import {TagList, sortedUsers, type User} from '../users';
declare global {
namespace jest {
interface Matchers<R> {
toHaveGoodDimensions: () => R;
}
}
}
expect.extend({
toHaveGoodDimensions({width, height}: {width: number; height: number}) {
// Put this one first because aspect ratio is harder to fix than resizing
// (need to take another screenshot)
if (width / height > 2) {
return {
pass: false,
message: () =>
`The preview image's width is ${width} and height is ${height}. To make sure it takes up the entire container in our showcase card, it needs to have an aspect ratio of no wider than 2:1. Please make your image taller.`,
};
} else if (width < 640) {
return {
pass: false,
message: () =>
`The preview image's width is ${width}, but we require a minimum 640. You can either resize it locally, or you can wait for the maintainer to resize it for you.`,
};
}
return {
pass: true,
message: () => "The preview image's dimensions are good",
};
},
});
describe('users data', () => {
it.each(sortedUsers)('$title', (user) => {
Joi.attempt(
user,
Joi.object<User>({
title: Joi.string().required(),
description: Joi.string()
.required()
.max(120)
.message(
'Please constrain your description text to maximum 120 characters.',
),
website: Joi.string()
.pattern(/^https?:\/\//)
.message('')
.required(),
// The preview should be jest/emptyModule
preview: Joi.object({default: Joi.any()})
.unknown(false)
.allow(null)
.required()
.messages({
'object.base':
'The image should be hosted on Docusaurus site, and not use remote HTTP or HTTPS URLs. It must be imported with require().',
}),
tags: Joi.array()
.items(...TagList)
.required(),
source: Joi.string().allow(null).required().messages({
'any.required':
"The source attribute is required.\nIf your Docusaurus site is not open-source, please make it explicit with 'source: null'.",
}),
}).unknown(false),
);
// cSpell:ignore opensource
if (user.tags.includes('opensource') && user.source === null) {
throw new Error(
"You can't add the 'opensource' tag to a site that does not have a link to source code. Please add your source code, or remove this tag.",
);
} else if (user.source !== null && !user.tags.includes('opensource')) {
throw new Error(
"For open-source sites, please add the 'opensource' tag.",
);
}
});
it('does not contain duplicates', () => {
function normalizeUrl(url: string | null) {
if (url === null) {
return null;
}
if (!url.endsWith('/')) {
return `${url}/`;
}
return url;
}
function duplicatesBy(mapper: (user: User) => string | null) {
const grouped: {[key: string]: User[]} = _.groupBy(sortedUsers, (user) =>
mapper(user),
);
return Object.fromEntries(
Object.entries(grouped).filter((entry) => entry[1].length > 1),
);
}
let duplicatesLog = '';
const duplicatesByTitle = duplicatesBy((user) =>
user.title.trim().toLowerCase(),
);
Object.entries(duplicatesByTitle).forEach(([title, users]) => {
duplicatesLog += `Showcase site title '${title}' is used ${users.length} times! Duplicates are not allowed!\n`;
});
const duplicatesByDescription = duplicatesBy((user) =>
user.description.trim().toLowerCase(),
);
Object.entries(duplicatesByDescription).forEach(([description, users]) => {
duplicatesLog += `Showcase site description '${description}' is used ${users.length} times! Duplicates are not allowed!\n`;
});
const duplicatesByWebsite = duplicatesBy((user) =>
normalizeUrl(user.website),
);
Object.entries(duplicatesByWebsite).forEach(([website, users]) => {
duplicatesLog += `Showcase site website url '${website}' is used ${users.length} times! Duplicates are not allowed!\n`;
});
const duplicatesBySource = duplicatesBy((user) =>
normalizeUrl(user.source),
);
Object.entries(duplicatesBySource).forEach(([source, users]) => {
// source is allowed to be null for multiple sites
// "null", see Lodash groupBy issue: https://github.com/lodash/lodash/issues/3060
if (source && source !== 'null') {
duplicatesLog += `Showcase site source url '${source}' is used ${users.length} times! Duplicates are not allowed!\n`;
}
});
if (duplicatesLog) {
throw new Error(duplicatesLog);
}
});
});
describe('preview images', () => {
const imageDir = path.join(__dirname, '../showcase');
// eslint-disable-next-line no-restricted-properties
const files = fs
.readdirSync(imageDir)
.filter((file) => ['.png', 'jpg', '.jpeg'].includes(path.extname(file)));
it.each(files)('%s', (file) => {
const size = imageSize(path.join(imageDir, file));
expect(size).toHaveGoodDimensions();
});
});