Skip to content

Commit

Permalink
test: plugin preferences saved with expected key
Browse files Browse the repository at this point in the history
Plugin preferences should be saved with the key defined in the name
property of the class
ref: https://ember-headless-table.pages.dev/api/interfaces/plugins.Plugin#name
  • Loading branch information
joelamb committed Sep 25, 2023
1 parent 263d12d commit f8b207d
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 2 deletions.
83 changes: 82 additions & 1 deletion test-app/tests/plugins/column-reordering/rendering-test.gts
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ module('Plugins | columnReordering', function (hooks) {
});
});

module('with a preferences adapter', function (hooks) {
module('with a preferences adapter and previously saved preferences', function (hooks) {
let preferences: null | PreferencesData = {};

class DefaultOptions extends Context {
Expand Down Expand Up @@ -532,4 +532,85 @@ module('Plugins | columnReordering', function (hooks) {
});
});

module('with a preferences adapter and no previously saved preferences', function (hooks) {
let preferences: null | PreferencesData = {};

class DefaultOptions extends Context {
table = headlessTable(this, {
columns: () => this.columns,
data: () => DATA,
plugins: [ColumnReordering, ColumnVisibility],
preferences: {
key: 'test-preferences',
adapter: {
persist: (_key: string, data: PreferencesData) => {
preferences = data;
},
restore: (key: string) => ({})
}
}
});
}

hooks.beforeEach(async function () {
preferences = null;
ctx = new DefaultOptions();
setOwner(ctx, this.owner);

await render(
// @ts-ignore
<template>
<TestComponentA @ctx={{ctx}} />
</template>
);
});

test('changing column order with `set all` updates preferences', async function (assert) {
assert.strictEqual(getColumnOrder(), 'A B C D', 'pre-test setup');

let order = new ColumnOrder({
columns: () =>
[
{ key: 'D' },
{ key: 'C' },
{ key: 'B' },
{ key: 'A' },
] as Column[],
existingOrder: new Map([
['A', 3],
['B', 2],
['C', 1],
['D', 0],
]),
});

// @ts-expect-error
setColumnOrder(ctx.table, order);

assert.deepEqual(preferences, {
"plugins": {
"column-reordering": {
"columns": {},
"table": {
"order": {
"A": 3,
"B": 2,
"C": 1,
"D": 0
}
}
},
"column-visibility": {
"columns": {
"A": {},
"B": {},
"C": {},
"D": {}
},
"table": {}
}
}
});
});
});
});
85 changes: 84 additions & 1 deletion test-app/tests/plugins/column-resizing/rendering-test.gts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ module('Plugins | resizing', function (hooks) {
});
});

module('with a preferences adapter', function (hooks) {
module('with a preferences adapter and previously saved preferences', function (hooks) {
let preferences: null | PreferencesData = {};

class DefaultOptions extends Context {
Expand Down Expand Up @@ -353,6 +353,89 @@ module('Plugins | resizing', function (hooks) {
});
});

module('with a preferences adapter and no previously saved preferences', function (hooks) {
let preferences: null | PreferencesData = {};

class DefaultOptions extends Context {
table = headlessTable(this, {
columns: () => this.columns,
data: () => [] as unknown[],
plugins: [ColumnResizing],
preferences: {
key: 'test-preferences',
adapter: {
persist: (_key: string, data: PreferencesData) => {
preferences = data;
},
restore: (key: string) => ({})
}
}
});
}

hooks.beforeEach(function () {
preferences = null;
ctx = new DefaultOptions();
setOwner(ctx, this.owner);
});

test('it resizes each column and persists the new widths in the preferences', async function (assert) {
ctx.setContainerWidth(1000);
await render(
<template>
<TestComponentA @ctx={{ctx}} />
</template>
)

const [columnA, columnB, columnC, columnD] = getColumns();

debugAssert(`columnA doesn't exist`, columnA);
debugAssert(`columnB doesn't exist`, columnB);
debugAssert(`columnC doesn't exist`, columnC);
debugAssert(`columnD doesn't exist`, columnD);

// Columns all have the same default width since there are no previusly saved preferences to restore
assert.equal(width(columnA), 250, 'col A has expected width before resize');
assert.equal(width(columnB), 250, 'col B has expected width before resize');
assert.equal(width(columnC), 250, 'col C has expected width before resize');
assert.equal(width(columnD), 250, 'col D has expected width before resize');

await requestAnimationFrameSettled();

// move the the resize handler between columns A & B 200px to the right
// increasing the width of column A and decreasing the width of columns
// to the right , while respecting the min width (128px)
await dragRight(columnB, 200);

assert.equal(width(columnA), 450, 'col A has expected width after resize');
assert.equal(width(columnB), 128, 'col B has expected width after resize');
assert.equal(width(columnC), 172, 'col C has expected width after resize');
assert.equal(width(columnD), 250, 'col D has expected width after resize');

assert.deepEqual(preferences, {
"plugins": {
"column-resizing": {
"columns": {
"A": {
"width": "450"
},
"B": {
"width": "128"
},
"C": {
"width": "172"
},
"D": {
"width": "250"
},
},
"table": {}
}
}
}, 'column widths saved in preferences');
});
});

module('with options that affect resize behavior', function (hooks) {
module('handlePosition (default)', function (hooks) {
class DefaultOptions extends Context {
Expand Down

0 comments on commit f8b207d

Please sign in to comment.