import { expect } from "chai";
import { initJSDOM } from "../../../util/jsdom.mjs";

let getStoredOrderConfigKey;
let getFilterConfigKey;
let getStoredFilterConfigKey;

describe("Datatable config keys", function () {
	before(function (done) {
		initJSDOM().then(() => {
			import("../../../../source/components/datatable/datatable.mjs")
				.then((m) => {
					getStoredOrderConfigKey = m.getStoredOrderConfigKey;
					return import(
						"../../../../source/components/datatable/filter/util.mjs"
					);
				})
				.then((m) => {
					getFilterConfigKey = m.getFilterConfigKey;
					getStoredFilterConfigKey = m.getStoredFilterConfigKey;
					done();
				})
				.catch(done);
		});
	});

	it("uses the same explicit instance-key contract for filter, stored filters and sorting", function () {
		const context = {
			id: "dom-id",
			getOption(path) {
				if (path === "config.instanceKey") {
					return "orders.default";
				}
			},
		};

		expect(getFilterConfigKey.call(context)).to.equal(
			"filter_datatable_orders_default",
		);
		expect(getStoredFilterConfigKey.call(context)).to.equal(
			"stored_filter_datatable_orders_default",
		);
		expect(getStoredOrderConfigKey.call(context)).to.equal(
			"stored_order_datatable_orders_default",
		);
	});
});
