mirror of
https://github.com/jellyfin/jellyfin-web
synced 2025-03-30 19:56:21 +00:00
Add unit tests for utils (#5412)
* add unit tests for Card utils * add unit tests for DateFnsLocale utils * fix lint * add unit tests for Events utils * fix lint * fix lint
This commit is contained in:
parent
f57c089ae2
commit
a133a33a00
3 changed files with 184 additions and 0 deletions
37
src/utils/card.test.ts
Normal file
37
src/utils/card.test.ts
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
import { describe, expect, it } from 'vitest';
|
||||||
|
import * as card from './card';
|
||||||
|
|
||||||
|
describe('Utils: card', () => {
|
||||||
|
describe('Function: getSquareShape', () => {
|
||||||
|
it('Should return "overflowSquare"', () => {
|
||||||
|
const result = card.getSquareShape(true);
|
||||||
|
expect(result).toEqual('overflowSquare');
|
||||||
|
});
|
||||||
|
it('Should return "square"', () => {
|
||||||
|
const result = card.getSquareShape(false);
|
||||||
|
expect(result).toEqual('square');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Function: getBackdropShape', () => {
|
||||||
|
it('Should return "overflowBackdrop"', () => {
|
||||||
|
const result = card.getBackdropShape(true);
|
||||||
|
expect(result).toEqual('overflowBackdrop');
|
||||||
|
});
|
||||||
|
it('Should return "backdrop"', () => {
|
||||||
|
const result = card.getBackdropShape(false);
|
||||||
|
expect(result).toEqual('backdrop');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Function: getPortraitShape', () => {
|
||||||
|
it('Should return "overflowPortrait"', () => {
|
||||||
|
const result = card.getPortraitShape(true);
|
||||||
|
expect(result).toEqual('overflowPortrait');
|
||||||
|
});
|
||||||
|
it('Should return "portrait"', () => {
|
||||||
|
const result = card.getPortraitShape(false);
|
||||||
|
expect(result).toEqual('portrait');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
58
src/utils/dateFnsLocale.test.ts
Normal file
58
src/utils/dateFnsLocale.test.ts
Normal file
|
@ -0,0 +1,58 @@
|
||||||
|
import { describe, expect, it } from 'vitest';
|
||||||
|
import * as dateFnsLocale from './dateFnsLocale';
|
||||||
|
|
||||||
|
describe('Utils: dateFnsLocale', () => {
|
||||||
|
describe('Function: getLocale', () => {
|
||||||
|
it('Should return "en-US" by default', () => {
|
||||||
|
const { code } = dateFnsLocale.getLocale();
|
||||||
|
expect(code).toEqual('en-US');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Function: getLocaleWithSuffix', () => {
|
||||||
|
it('Should return "en-US" by default with addSuffix to true', () => {
|
||||||
|
const { addSuffix, locale } = dateFnsLocale.getLocaleWithSuffix();
|
||||||
|
|
||||||
|
expect(addSuffix).toEqual(true);
|
||||||
|
expect(locale.code).toEqual('en-US');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Function: updateLocale', () => {
|
||||||
|
it('Should import "fr-ca" locale', async () => {
|
||||||
|
const expectedCode = 'fr-CA';
|
||||||
|
|
||||||
|
await dateFnsLocale.updateLocale('fr-ca');
|
||||||
|
const { code } = dateFnsLocale.getLocale();
|
||||||
|
const { locale: localeWithSuffix } =
|
||||||
|
dateFnsLocale.getLocaleWithSuffix();
|
||||||
|
|
||||||
|
expect(code).toEqual(expectedCode);
|
||||||
|
expect(localeWithSuffix.code).toEqual(expectedCode);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Should import "fr" locale', async () => {
|
||||||
|
const expectedCode = 'fr';
|
||||||
|
|
||||||
|
await dateFnsLocale.updateLocale('fr-fr');
|
||||||
|
const { code } = dateFnsLocale.getLocale();
|
||||||
|
const { locale: localeWithSuffix } =
|
||||||
|
dateFnsLocale.getLocaleWithSuffix();
|
||||||
|
|
||||||
|
expect(code).toEqual(expectedCode);
|
||||||
|
expect(localeWithSuffix.code).toEqual(expectedCode);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Should import "en-US" locale if given locale is not found', async () => {
|
||||||
|
const expectedCode = 'en-US';
|
||||||
|
|
||||||
|
await dateFnsLocale.updateLocale('unknown-unknown');
|
||||||
|
const { code } = dateFnsLocale.getLocale();
|
||||||
|
const { locale: localeWithSuffix } =
|
||||||
|
dateFnsLocale.getLocaleWithSuffix();
|
||||||
|
|
||||||
|
expect(code).toEqual(expectedCode);
|
||||||
|
expect(localeWithSuffix.code).toEqual(expectedCode);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
89
src/utils/events.test.ts
Normal file
89
src/utils/events.test.ts
Normal file
|
@ -0,0 +1,89 @@
|
||||||
|
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||||
|
import eventsUtils from './events';
|
||||||
|
|
||||||
|
describe('Utils: events', () => {
|
||||||
|
describe('Method: on', () => {
|
||||||
|
it('should throw error if object is null', () => {
|
||||||
|
const call = () => eventsUtils.on(null, 'testEvent', vi.fn());
|
||||||
|
|
||||||
|
expect(call).toThrowError(new Error('obj cannot be null!'));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should init object callbacks with testEvent type if it does not exist', () => {
|
||||||
|
const obj = {};
|
||||||
|
const callback = vi.fn();
|
||||||
|
|
||||||
|
eventsUtils.on(obj, 'testEvent', callback);
|
||||||
|
|
||||||
|
expect(obj).toHaveProperty('_callbacks', {
|
||||||
|
testEvent: [callback]
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should add callback to existing object callbacks', () => {
|
||||||
|
const initialCallback = vi.fn();
|
||||||
|
const obj = {
|
||||||
|
_callbacks: { testEvent: [initialCallback] }
|
||||||
|
};
|
||||||
|
const otherCallback = vi.fn();
|
||||||
|
|
||||||
|
eventsUtils.on(obj, 'testEvent', otherCallback);
|
||||||
|
|
||||||
|
expect(obj).toHaveProperty('_callbacks', {
|
||||||
|
testEvent: [initialCallback, otherCallback]
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Method: off', () => {
|
||||||
|
let obj: object;
|
||||||
|
let initialCallback: ReturnType<typeof vi.fn>;
|
||||||
|
beforeEach(() => {
|
||||||
|
initialCallback = vi.fn();
|
||||||
|
obj = {
|
||||||
|
_callbacks: {
|
||||||
|
testEvent: [initialCallback]
|
||||||
|
}
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should remove existing callbacks', () => {
|
||||||
|
eventsUtils.off(obj, 'testEvent', initialCallback);
|
||||||
|
|
||||||
|
expect(obj).toHaveProperty('_callbacks', { testEvent: [] });
|
||||||
|
});
|
||||||
|
it('should not remove callback if it is not registered for the given event', () => {
|
||||||
|
eventsUtils.off(obj, 'otherEvent', initialCallback);
|
||||||
|
|
||||||
|
expect(obj).toHaveProperty('_callbacks', {
|
||||||
|
testEvent: [initialCallback],
|
||||||
|
otherEvent: []
|
||||||
|
});
|
||||||
|
});
|
||||||
|
it('should not remove callback if it is not registered', () => {
|
||||||
|
const callbackToRemove = vi.fn();
|
||||||
|
|
||||||
|
eventsUtils.off(obj, 'testEvent', callbackToRemove);
|
||||||
|
|
||||||
|
expect(obj).toHaveProperty('_callbacks', {
|
||||||
|
testEvent: [initialCallback]
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Method: trigger', () => {
|
||||||
|
it('should trigger registered callback with given parameters', () => {
|
||||||
|
const obj = {};
|
||||||
|
const callback = vi.fn();
|
||||||
|
eventsUtils.on(obj, 'testEvent', callback);
|
||||||
|
|
||||||
|
eventsUtils.trigger(obj, 'testEvent', ['testValue1', 'testValue2']);
|
||||||
|
|
||||||
|
expect(callback).toHaveBeenCalledWith(
|
||||||
|
{ type: 'testEvent' },
|
||||||
|
'testValue1',
|
||||||
|
'testValue2'
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
Loading…
Add table
Add a link
Reference in a new issue