Skip to content

Commit

Permalink
NEXT-26011 - add tests for admin filter
Browse files Browse the repository at this point in the history
  • Loading branch information
jleifeld committed Apr 21, 2023
1 parent 62755fc commit fa01d12
Show file tree
Hide file tree
Showing 26 changed files with 454 additions and 89 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
describe('src/app/filter/asset.filter.ts', () => {
const assetFilter = Shopware.Filter.getByName('asset');

beforeEach(() => {
Shopware.Context.api.assetsPath = '';
});

it('should contain a filter', () => {
expect(assetFilter).toBeDefined();
});

it('should return empty string when no value is given', () => {
const result = assetFilter();

expect(result).toBe('');
});

it('should remove the first slash because double slashes does not work on external storage like s3', () => {
const result = assetFilter('/test.jpg');

expect(result).toBe('test.jpg');
});

it('should use the assetsPath from the Context API', () => {
Shopware.Context.api.assetsPath = 'https://www.shopware.com/';
const result = assetFilter('/test.jpg');

expect(result).toBe('https://www.shopware.com/test.jpg');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ Shopware.Filter.register('asset', (value: string) => {
* @deprecated tag:v6.6.0 - Will be private
*/
export {};

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* @package admin
*/
import type { CurrencyOptions } from 'src/core/service/utils/format.utils';

const { currency } = Shopware.Utils.format;

/**
* @deprecated tag:v6.6.0 - Will be private
*/
Shopware.Filter.register('currency', (
value: string|boolean,
format: string,
decimalPlaces: number,
additionalOptions: CurrencyOptions,
) => {
if ((!value || value === true) && (!Shopware.Utils.types.isNumber(value) || Shopware.Utils.types.isEqual(value, NaN))) {
return '-';
}

if (Shopware.Utils.types.isEqual(parseInt(value, 10), NaN)) {
return value;
}

return currency(parseFloat(value), format, decimalPlaces, additionalOptions);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
describe('src/app/filter/date.filter.ts', () => {
const dateFilter = Shopware.Filter.getByName('date');

Shopware.Utils.format.date = jest.fn();

beforeEach(() => {
Shopware.Utils.format.date.mockClear();
});

it('should contain a filter', () => {
expect(dateFilter).toBeDefined();
});

it('should return empty string when no value is given', () => {
expect(dateFilter()).toBe('');
});

it('should call the date format util for formatting', () => {
dateFilter('01.01.1997', {
myDateOptions: 'foo',
});

expect(Shopware.Utils.format.date).toHaveBeenCalledWith(
'01.01.1997',
{
myDateOptions: 'foo',
},
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@
* @package admin
*/

const { Filter } = Shopware;
const { date } = Shopware.Utils.format;

Filter.register('date', (value: string, options: Intl.DateTimeFormatOptions = {}): string => {
Shopware.Filter.register('date', (value: string, options: Intl.DateTimeFormatOptions = {}): string => {
if (!value) {
return '';
}

return date(value, options);
return Shopware.Utils.format.date(value, options);
});

/**
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
describe('src/app/filter/file-size.filter.js', () => {
const fileSizeFilter = Shopware.Filter.getByName('fileSize');

Shopware.Utils.format.fileSize = jest.fn();

beforeEach(() => {
Shopware.Utils.format.fileSize.mockClear();
});

it('should contain a filter', () => {
expect(fileSizeFilter).toBeDefined();
});

it('should return empty string when no value is given', () => {
expect(fileSizeFilter()).toBe('');
});

it('should call the fileSize format util for formatting', () => {
fileSizeFilter(
1856165,
{
myLocaleOptions: 'foo',
},
);

expect(Shopware.Utils.format.fileSize).toHaveBeenCalledWith(
1856165,
{
myLocaleOptions: 'foo',
},
);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* @package admin
*/

/**
* @deprecated tag:v6.6.0 - Will be private
*/
Shopware.Filter.register('fileSize', (value: number, locale: string) => {
if (!value) {
return '';
}

return Shopware.Utils.format.fileSize(value, locale);
});

/* @private */
export {};

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
describe('src/app/filter/media-name.filter.js', () => {
const mediaNameFilter = Shopware.Filter.getByName('mediaName');

it('should contain a filter', () => {
expect(mediaNameFilter).toBeDefined();
});

it('should return empty string fallback when no value is given', () => {
expect(mediaNameFilter()).toBe('');
});

it('should return given fallback when no value is given', () => {
expect(mediaNameFilter(undefined, 'fooBar')).toBe('fooBar');
});

it('should return the values inside the entity by default', () => {
expect(mediaNameFilter({
entity: {
fileName: 'my-file-name',
fileExtension: 'jpg',
},
})).toBe('my-file-name.jpg');
});

it('should return the values even when not entity is given', () => {
expect(mediaNameFilter({
fileName: 'my-file-name',
fileExtension: 'jpg',
})).toBe('my-file-name.jpg');
});

it('should return the fallback when fileName or fileExtension is missing', () => {
expect(mediaNameFilter(
{
fileNameFoo: 'my-file-name',
fileExtensionBar: 'jpg',
},
'my-fallback',
)).toBe('my-fallback');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* @package content
*/
Shopware.Filter.register('mediaName', (
value: {
entity?: {
fileName?: string,
fileExtension?: string
},
fileName?: string,
fileExtension?: string
},
// eslint-disable-next-line @typescript-eslint/no-inferrable-types
fallback: string = '',
): string => {
if (!value) {
return fallback;
}

if (value.entity) {
value = value.entity;
}

if ((!value.fileName) || (!value.fileExtension)) {
return fallback;
}

return `${value.fileName}.${value.fileExtension}`;
});

/* @private */
export {};
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
describe('src/app/filter/salutation.filter.ts', () => {
const salutationFilter = Shopware.Filter.getByName('salutation');

it('should contain a filter', () => {
expect(salutationFilter).toBeDefined();
});

it('should return empty string fallback when no value is given', () => {
expect(salutationFilter()).toBe('');
});

it('should return given fallback when no value is given', () => {
expect(salutationFilter(undefined, 'fooBar')).toBe('fooBar');
});

it('should return the correct salutation', () => {
expect(salutationFilter({
salutation: {
id: '1',
salutationKey: 'mr',
displayName: 'Mr.',
},
title: 'Dr.',
firstName: 'Max',
lastName: 'Mustermann',
})).toBe('Mr. Dr. Max Mustermann');
});

it('should hide salutation when no salutationKey was defined', () => {
expect(salutationFilter({
salutation: {
id: '1',
salutationKey: 'not_specified',
displayName: 'Mr.',
},
title: 'Dr.',
firstName: 'Max',
lastName: 'Mustermann',
})).toBe('Dr. Max Mustermann');
});

it('should return the fallback snippet when no subvalues are given', () => {
expect(salutationFilter({
salutation: {
id: '1',
salutationKey: 'mr',
displayName: '',
},
title: '',
firstName: '',
lastName: '',
})).toBe('');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ Filter.register('salutation', (
* @deprecated tag:v6.6.0 - Will be private
*/
export default {};

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
describe('src/app/filter/stock-color-variant.filter.ts', () => {
const stockColorVariantFilter = Shopware.Filter.getByName('stockColorVariant');

it('should contain a filter', () => {
expect(stockColorVariantFilter).toBeDefined();
});

it('should return empty string fallback when no value is given', () => {
expect(stockColorVariantFilter()).toBe('');
});

it('should return success when value is 25 or higher', () => {
expect(stockColorVariantFilter(25)).toBe('success');
expect(stockColorVariantFilter(29)).toBe('success');
});

it('should return warning when value is between 1 and 25', () => {
expect(stockColorVariantFilter(1)).toBe('warning');
expect(stockColorVariantFilter(18)).toBe('warning');
expect(stockColorVariantFilter(24)).toBe('warning');
});

it('should return error when value is below 0', () => {
expect(stockColorVariantFilter(0)).toBe('error');
expect(stockColorVariantFilter(-5)).toBe('error');
});
});
Loading

0 comments on commit fa01d12

Please sign in to comment.