refactor(commands): Use Locale over LocaleString (#10624)

* refactor: use `Locale`

* test: update `LocaleString` tests
This commit is contained in:
Jiralite
2024-11-28 17:33:40 +00:00
committed by GitHub
parent 2b0944a92f
commit 9d62ff57d6
4 changed files with 49 additions and 35 deletions

View File

@@ -3,6 +3,7 @@ import {
ApplicationIntegrationType,
ChannelType,
InteractionContextType,
Locale,
PermissionFlagsBits,
} from 'discord-api-types/v10';
import { describe, test, expect } from 'vitest';
@@ -376,15 +377,15 @@ describe('ChatInput Commands', () => {
});
describe('ChatInput command localizations', () => {
const expectedSingleLocale = { 'en-US': 'foobar' };
const expectedSingleLocale = { [Locale.EnglishUS]: 'foobar' };
const expectedMultipleLocales = {
...expectedSingleLocale,
bg: 'test',
};
test('GIVEN valid name localizations THEN does not throw error', () => {
expect(() => getBuilder().setNameLocalization('en-US', 'foobar')).not.toThrowError();
expect(() => getBuilder().setNameLocalizations({ 'en-US': 'foobar' })).not.toThrowError();
expect(() => getBuilder().setNameLocalization(Locale.EnglishUS, 'foobar')).not.toThrowError();
expect(() => getBuilder().setNameLocalizations({ [Locale.EnglishUS]: 'foobar' })).not.toThrowError();
});
test('GIVEN invalid name localizations THEN does throw error', () => {
@@ -395,21 +396,29 @@ describe('ChatInput Commands', () => {
});
test('GIVEN valid name localizations THEN valid data is stored', () => {
expect(getNamedBuilder().setNameLocalization('en-US', 'foobar').toJSON().name_localizations).toEqual(
expect(getNamedBuilder().setNameLocalization(Locale.EnglishUS, 'foobar').toJSON().name_localizations).toEqual(
expectedSingleLocale,
);
expect(
getNamedBuilder().setNameLocalizations({ 'en-US': 'foobar', bg: 'test' }).toJSON().name_localizations,
getNamedBuilder()
.setNameLocalizations({ [Locale.EnglishUS]: 'foobar', bg: 'test' })
.toJSON().name_localizations,
).toEqual(expectedMultipleLocales);
expect(getNamedBuilder().clearNameLocalizations().toJSON().name_localizations).toBeUndefined();
expect(getNamedBuilder().clearNameLocalization('en-US').toJSON().name_localizations).toEqual({
'en-US': undefined,
expect(getNamedBuilder().clearNameLocalization(Locale.EnglishUS).toJSON().name_localizations).toEqual({
[Locale.EnglishUS]: undefined,
});
});
test('GIVEN valid description localizations THEN does not throw error', () => {
expect(() => getNamedBuilder().setDescriptionLocalization('en-US', 'foobar').toJSON()).not.toThrowError();
expect(() => getNamedBuilder().setDescriptionLocalizations({ 'en-US': 'foobar' }).toJSON()).not.toThrowError();
expect(() =>
getNamedBuilder().setDescriptionLocalization(Locale.EnglishUS, 'foobar').toJSON(),
).not.toThrowError();
expect(() =>
getNamedBuilder()
.setDescriptionLocalizations({ [Locale.EnglishUS]: 'foobar' })
.toJSON(),
).not.toThrowError();
});
test('GIVEN invalid description localizations THEN does throw error', () => {
@@ -421,20 +430,22 @@ describe('ChatInput Commands', () => {
test('GIVEN valid description localizations THEN valid data is stored', () => {
expect(
getNamedBuilder().setDescriptionLocalization('en-US', 'foobar').toJSON(false).description_localizations,
getNamedBuilder().setDescriptionLocalization(Locale.EnglishUS, 'foobar').toJSON(false)
.description_localizations,
).toEqual(expectedSingleLocale);
expect(
getNamedBuilder().setDescriptionLocalizations({ 'en-US': 'foobar', bg: 'test' }).toJSON(false)
.description_localizations,
getNamedBuilder()
.setDescriptionLocalizations({ [Locale.EnglishUS]: 'foobar', bg: 'test' })
.toJSON(false).description_localizations,
).toEqual(expectedMultipleLocales);
expect(
getNamedBuilder().clearDescriptionLocalizations().toJSON(false).description_localizations,
).toBeUndefined();
expect(getNamedBuilder().clearDescriptionLocalization('en-US').toJSON(false).description_localizations).toEqual(
{
'en-US': undefined,
},
);
expect(
getNamedBuilder().clearDescriptionLocalization(Locale.EnglishUS).toJSON(false).description_localizations,
).toEqual({
[Locale.EnglishUS]: undefined,
});
});
});

View File

@@ -2,6 +2,7 @@ import {
ApplicationCommandType,
ApplicationIntegrationType,
InteractionContextType,
Locale,
PermissionFlagsBits,
} from 'discord-api-types/v10';
import { describe, test, expect } from 'vitest';
@@ -36,15 +37,15 @@ describe('Context Menu Commands', () => {
});
describe('Context menu command localizations', () => {
const expectedSingleLocale = { 'en-US': 'foobar' };
const expectedSingleLocale = { [Locale.EnglishUS]: 'foobar' };
const expectedMultipleLocales = {
...expectedSingleLocale,
bg: 'test',
};
test('GIVEN valid name localizations THEN does not throw error', () => {
expect(() => getBuilder().setNameLocalization('en-US', 'foobar')).not.toThrowError();
expect(() => getBuilder().setNameLocalizations({ 'en-US': 'foobar' })).not.toThrowError();
expect(() => getBuilder().setNameLocalization(Locale.EnglishUS, 'foobar')).not.toThrowError();
expect(() => getBuilder().setNameLocalizations({ [Locale.EnglishUS]: 'foobar' })).not.toThrowError();
});
test('GIVEN invalid name localizations THEN does throw error', () => {
@@ -55,16 +56,18 @@ describe('Context Menu Commands', () => {
});
test('GIVEN valid name localizations THEN valid data is stored', () => {
expect(getBuilder().setName('hi').setNameLocalization('en-US', 'foobar').toJSON().name_localizations).toEqual(
expectedSingleLocale,
);
expect(
getBuilder().setName('hi').setNameLocalizations({ 'en-US': 'foobar', bg: 'test' }).toJSON()
.name_localizations,
getBuilder().setName('hi').setNameLocalization(Locale.EnglishUS, 'foobar').toJSON().name_localizations,
).toEqual(expectedSingleLocale);
expect(
getBuilder()
.setName('hi')
.setNameLocalizations({ [Locale.EnglishUS]: 'foobar', bg: 'test' })
.toJSON().name_localizations,
).toEqual(expectedMultipleLocales);
expect(getBuilder().setName('hi').clearNameLocalizations().toJSON().name_localizations).toBeUndefined();
expect(getBuilder().setName('hi').clearNameLocalization('en-US').toJSON().name_localizations).toEqual({
'en-US': undefined,
expect(getBuilder().setName('hi').clearNameLocalization(Locale.EnglishUS).toJSON().name_localizations).toEqual({
[Locale.EnglishUS]: undefined,
});
});
});

View File

@@ -1,4 +1,4 @@
import type { LocaleString, RESTPostAPIApplicationCommandsJSONBody } from 'discord-api-types/v10';
import type { Locale, RESTPostAPIApplicationCommandsJSONBody } from 'discord-api-types/v10';
export interface SharedNameData
extends Partial<Pick<RESTPostAPIApplicationCommandsJSONBody, 'name_localizations' | 'name'>> {}
@@ -25,7 +25,7 @@ export class SharedName {
* @param locale - The locale to set
* @param localizedName - The localized name for the given `locale`
*/
public setNameLocalization(locale: LocaleString, localizedName: string) {
public setNameLocalization(locale: Locale, localizedName: string) {
this.data.name_localizations ??= {};
this.data.name_localizations[locale] = localizedName;
@@ -37,7 +37,7 @@ export class SharedName {
*
* @param locale - The locale to clear
*/
public clearNameLocalization(locale: LocaleString) {
public clearNameLocalization(locale: Locale) {
this.data.name_localizations ??= {};
this.data.name_localizations[locale] = undefined;
@@ -49,7 +49,7 @@ export class SharedName {
*
* @param localizedNames - The object of localized names to set
*/
public setNameLocalizations(localizedNames: Partial<Record<LocaleString, string>>) {
public setNameLocalizations(localizedNames: Partial<Record<Locale, string>>) {
this.data.name_localizations = structuredClone(localizedNames);
return this;
}

View File

@@ -1,4 +1,4 @@
import type { APIApplicationCommand, LocaleString } from 'discord-api-types/v10';
import type { APIApplicationCommand, Locale } from 'discord-api-types/v10';
import type { SharedNameData } from './SharedName.js';
import { SharedName } from './SharedName.js';
@@ -28,7 +28,7 @@ export class SharedNameAndDescription extends SharedName {
* @param locale - The locale to set
* @param localizedDescription - The localized description for the given `locale`
*/
public setDescriptionLocalization(locale: LocaleString, localizedDescription: string) {
public setDescriptionLocalization(locale: Locale, localizedDescription: string) {
this.data.description_localizations ??= {};
this.data.description_localizations[locale] = localizedDescription;
@@ -40,7 +40,7 @@ export class SharedNameAndDescription extends SharedName {
*
* @param locale - The locale to clear
*/
public clearDescriptionLocalization(locale: LocaleString) {
public clearDescriptionLocalization(locale: Locale) {
this.data.description_localizations ??= {};
this.data.description_localizations[locale] = undefined;
@@ -52,7 +52,7 @@ export class SharedNameAndDescription extends SharedName {
*
* @param localizedDescriptions - The object of localized descriptions to set
*/
public setDescriptionLocalizations(localizedDescriptions: Partial<Record<LocaleString, string>>) {
public setDescriptionLocalizations(localizedDescriptions: Partial<Record<Locale, string>>) {
this.data.description_localizations = structuredClone(localizedDescriptions);
return this;
}