Skip to content

Commit

Permalink
test(widgets): add general-purpose tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Haroenv committed Jan 13, 2021
1 parent 1f73444 commit eb4fc02
Showing 1 changed file with 164 additions and 0 deletions.
164 changes: 164 additions & 0 deletions src/widgets/__tests__/index.tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
import { PlacesInstance } from 'places.js';
import * as widgets from '../';
import { entries } from '../../lib/utils/typedObject';
import { Widget } from '../../types';

type Widgets = typeof widgets;
type WidgetNames = keyof typeof widgets;
function initiateAllWidgets(): Array<[WidgetNames, Widget]> {
return entries(widgets).map(([name, widget]) => {
return [name, initiateWidget(name, widget)];
});

function initiateWidget<TName extends WidgetNames>(
name: TName,
widget: Widgets[TName]
) {
const container = document.createElement('div');

switch (name) {
case 'index': {
const index = widget as Widgets['index'];
return index({ indexName: 'index' });
}
case 'EXPERIMENTAL_configureRelatedItems': {
const EXPERIMENTAL_configureRelatedItems = widget as Widgets['EXPERIMENTAL_configureRelatedItems'];
return EXPERIMENTAL_configureRelatedItems({
hit: { objectID: 'x' },
matchingPatterns: {},
});
}
case 'geoSearch': {
const geoSearch = widget as Widgets['geoSearch'];
return geoSearch({
container,
googleReference: { maps: { OverlayView: class OverlayView {} } },
});
}
case 'hierarchicalMenu': {
const hierarchicalMenu = widget as Widgets['hierarchicalMenu'];
return hierarchicalMenu({
container,
attributes: ['attr1', 'attr2'],
});
}
case 'breadcrumb': {
const breadcrumb = widget as Widgets['breadcrumb'];
return breadcrumb({
container,
attributes: ['attr1', 'attr2'],
});
}
case 'hitsPerPage': {
const hitsPerPage = widget as Widgets['hitsPerPage'];
return hitsPerPage({
container,
items: [{ default: true, label: 'def', value: 1 }],
});
}
case 'numericMenu': {
const numericMenu = widget as Widgets['numericMenu'];
return numericMenu({
container,
attribute: 'attr',
items: [{ label: 'x', start: 1, end: 2 }],
});
}
case 'sortBy': {
const sortBy = widget as Widgets['sortBy'];
return sortBy({
container,
items: [{ label: 'x', value: 'x' }],
});
}
case 'analytics': {
const analytics = widget as Widgets['analytics'];
return analytics({
pushFunction() {},
});
}
case 'queryRuleContext': {
const queryRuleContext = widget as Widgets['queryRuleContext'];
return queryRuleContext({
trackedFilters: {
facet(values) {
return values;
},
},
});
}
case 'places': {
const places = widget as Widgets['places'];
return places({
container: document.createElement('input'),
placesReference: () => ({} as PlacesInstance),
});
}
case 'panel': {
const panel = widget as Widgets['panel'];
return panel()(widgets.hierarchicalMenu)({
container,
attributes: ['attr1', 'attr2'],
});
}
default: {
return widget({ container, attribute: 'attr' });
}
}
}
}

describe('widgets', () => {
describe('$$type', () => {
test('present in every widget', () => {
const widgetInstances = initiateAllWidgets();

widgetInstances.forEach(([name, widget]) =>
expect([name, widget.$$type]).toEqual([name, expect.any(String)])
);
});

test('starts with ais.', () => {
const widgetInstances = initiateAllWidgets();

widgetInstances.forEach(([name, widget]) =>
expect([name, widget.$$type!.substr(0, 4)]).toEqual([name, 'ais.'])
);
});
});

describe('$$widgetType', () => {
test('present in all widgets', () => {
const widgetInstances = initiateAllWidgets();

widgetInstances.forEach(([name, widget]) =>
expect([name, widget.$$widgetType]).toEqual([name, expect.any(String)])
);
});

test('starts with ais.', () => {
const widgetInstances = initiateAllWidgets();

widgetInstances.forEach(([name, widget]) =>
expect([name, widget.$$widgetType!.substr(0, 4)]).toEqual([
name,
'ais.',
])
);
});

test('is the same as name', () => {
const widgetInstances = initiateAllWidgets();

widgetInstances.forEach(([name, widget]) => {
if (name === 'panel') {
// the widget type of panel is the wrapped widget
return;
}
expect(widget.$$widgetType).toBe(
`ais.${name.replace('EXPERIMENTAL_', '')}`
);
});
});
});
});

0 comments on commit eb4fc02

Please sign in to comment.