Is it possible to override the Site object with own fixture during Front End tests of a Plugin?

I’m currently working on this PR: FIX: presenting formatted location on User Card by merefield · Pull Request #73 · paviliondev/discourse-locations · GitHub

I’m attempting to cover the change with a new Front End test.

But the test fails to run because a required data attribute is not defined.

The issue I’m facing is how to successfully override the data within Site with a fixture held here:

which contains an additional attribute Site holds when Locations Plugin is present.

… and make it persist for the rest of the test!

The new test is here.

I successfully load Site with the fixture, apparently.

I stop at this debugger point during the qunit run in the browser and I can tell it’s correctly loaded:

  test("user card location - shows correct format", async function (assert) {
    const siteAttrs = {
      ...siteFixtures["site.json"].site,
    };
    PreloadStore.store("site", cloneJSON(siteAttrs));
    Site.resetCurrent();
    debugger;
    PreloadStore.get("site")

image

but when the User Card is clicked, the data is not what you’d expect and critically, the country_codes attribute is missing, so the test grinds to a halt, here:

Why is my overridden Site data getting lost?

1 Like

OK, this is frustrating.

It looks as though the value of the Site object is set here:

And cannot be overridden?

I’ve checked this out by modifying the fixture in core (here: discourse/site-fixtures.js at 93a4012ecb3af7306de0c51283d6dd2b2e90818c · discourse/discourse · GitHub) and it picks up my modification.

But I don’t want that, I want to override it in my plugin test with a fixture stored in my plugin.

To re-iterate, I’m able to do that for the scope of the test code but once the plugin code is running, the scope appears to pick up the site object that relates to the fixture loaded by the core test helper …

:thinking:

I finally solved this. Worked out you can use needs just as you can for other objects:

Clue here:

Final code:

  needs.site(cloneJSON(siteFixtures["country_codes.json"]));
3 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.