Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions website/src/data/__tests__/user.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

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';
Expand Down Expand Up @@ -62,6 +63,7 @@ describe('users data', () => {
// The preview should be jest/emptyModule
preview: Joi.object({default: Joi.any()})
.unknown(false)
.allow(null)
.required()
.messages({
'object.base':
Expand All @@ -87,6 +89,65 @@ describe('users data', () => {
);
}
});

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', () => {
Expand Down
Loading