Skip to content

Commit

Permalink
chore(types): fix up tests for new types, and add more type tests for…
Browse files Browse the repository at this point in the history
… plugin APIs
  • Loading branch information
NullVoxPopuli committed Oct 18, 2022
1 parent 791848e commit 308f2b5
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 10 deletions.
16 changes: 16 additions & 0 deletions ember-headless-table/src/-private/-type-tests/table-api.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { expectTypeOf } from 'expect-type';

import { ColumnReordering } from '../../plugins/column-reordering';
import { ColumnResizing } from '../../plugins/column-resizing';
import { DataSorting } from '../../plugins/data-sorting';

import type { Table } from '[public-types]';

// We're testing types, not behaviors
const x = 0 as unknown as Table<{ x: number }>;

//////////////////////////////
// <Table>#pluginOf
expectTypeOf(x.pluginOf(DataSorting)).toEqualTypeOf<DataSorting | undefined>();
expectTypeOf(x.pluginOf(ColumnReordering)).toEqualTypeOf<ColumnReordering | undefined>();
expectTypeOf(x.pluginOf(ColumnResizing)).toEqualTypeOf<ColumnResizing | undefined>();
19 changes: 12 additions & 7 deletions ember-headless-table/src/-private/-type-tests/table-config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import { expectTypeOf } from 'expect-type';

import { BasePlugin } from '../../plugins/-private/base';
import { ColumnReordering } from '../../plugins/column-reordering';
import { Plugins } from '../../plugins/-private/utils';
import { DataSorting, SortItem } from '../../plugins/data-sorting';
import { ColumnResizing } from '../../plugins/column-resizing';
import { ColumnVisibility } from '../../plugins/column-visibility';
import { DataSorting } from '../../plugins/data-sorting';
import { StickyColumns } from '../../plugins/sticky-columns';

import type { Plugins } from '../../plugins/-private/utils';
import type { SortItem } from '../../plugins/data-sorting';
import type { Plugin } from '[public-plugin-types]';
import type { TableConfig } from '[public-types]';
import type { Constructor } from '#private-types';
Expand All @@ -26,8 +27,10 @@ expectTypeOf<
[Constructor<BasePlugin>, Constructor<BasePlugin>]
>().toMatchTypeOf<TablePluginConfig>();

class SomeClass { foo = 'bar' }
class LocalPlugin extends BasePlugin<{ Meta: { Table: SomeClass} }> {
class SomeClass {
foo = 'bar';
}
class LocalPlugin extends BasePlugin<{ Meta: { Table: SomeClass } }> {
name = 'local-plugin';
}

Expand All @@ -51,8 +54,10 @@ expectTypeOf([StickyColumns]).toMatchTypeOf<TablePluginConfig>();
// The various ways to define plugins
expectTypeOf([DataSorting, ColumnReordering]).toMatchTypeOf<TablePluginConfig>();

const onSort = (_sorts: SortItem<number>[]) => {};
const onSort = (_sorts: SortItem<number>[]) => {
/* intentionally empty */
};
const sorts: SortItem<number>[] = [];
expectTypeOf([DataSorting.with(() => ({ }))]).toMatchTypeOf<TablePluginConfig>();
expectTypeOf([DataSorting.with(() => ({ onSort, sorts }))]).toMatchTypeOf<TablePluginConfig>();

expectTypeOf([DataSorting.with(() => ({}))]).toMatchTypeOf<TablePluginConfig>();
expectTypeOf([DataSorting.with(() => ({ onSort, sorts }))]).toMatchTypeOf<TablePluginConfig>();
2 changes: 1 addition & 1 deletion ember-headless-table/src/-private/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ export class Table<DataType = unknown> extends Resource<Signature<DataType>> {
/**
* Get the active plugin instance for the given plugin class
*/
pluginOf<Instance extends BasePlugin>(klass: Class<Instance>): Instance | undefined {
pluginOf<Instance extends BasePlugin<any>>(klass: Class<Instance>): Instance | undefined {
let result = this.plugins.find((plugin) => plugin instanceof klass);

/**
Expand Down
14 changes: 12 additions & 2 deletions test-app/tests/plugins/queries/meta-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,13 +230,23 @@ module('Plugins | Queries | meta', function (hooks) {
});

module('withFeature', function () {
class FeatureProvidingPlugin extends BasePlugin {
class FeatureProvidingPlugin extends BasePlugin<{
Meta: {
Table: TestTableMeta;
Column: TestColumnMeta;
};
}> {
static features = ['feature-a'];
name = 'queries-feature-a-test-plugin';
meta = { column: TestColumnMeta, table: TestTableMeta };
}

class FeatureProvidingPlugin2 extends BasePlugin {
class FeatureProvidingPlugin2 extends BasePlugin<{
Meta: {
Table: TestTableMeta;
Column: TestColumnMeta;
};
}> {
static features = ['feature-b'];
name = 'queries-feature-b-test-plugin';
meta = { column: TestColumnMeta, table: TestTableMeta };
Expand Down

0 comments on commit 308f2b5

Please sign in to comment.