merefield
(Robert)
December 16, 2022, 6:32pm
1
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:
paviliondev:main
← paviliondev:production_fixes
opened 03:54PM - 14 Dec 22 UTC
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.
paviliondev:main
← paviliondev:production_fixes
opened 03:54PM - 14 Dec 22 UTC
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")
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:
if (user && user.geo_location) {
contents.push(iconNode('map-marker-alt'));
let format = this.siteSettings.location_user_profile_format.split('|');
let opts = {};
let userLocation;
if (format.length && format[0]) {
opts['geoAttrs'] = format;
userLocation = geoLocationFormat(user.geo_location, self.site.country_codes, opts);
} else {
userLocation = user.geo_location.address;
};
contents.push(h('div.location-label', userLocation));
if (this.siteSettings.location_user_profile_map) {
let mapContents = [];
let btnParams = {
Why is my overridden Site data getting lost?
1 Like
merefield
(Robert)
December 20, 2022, 9:49pm
2
OK, this is frustrating.
It looks as though the value of the Site object is set here:
const clock = fakeTime(timeString, timezone, false);
try {
callback();
} finally {
clock.restore();
}
}
let _pretenderCallbacks = {};
export function resetSite(extras = {}) {
const siteAttrs = {
...siteFixtures["site.json"].site,
...extras,
};
PreloadStore.store("site", cloneJSON(siteAttrs));
Site.resetCurrent();
}
export function applyPretender(name, server, helper) {
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 …
merefield
(Robert)
December 28, 2022, 9:57pm
3
I finally solved this. Worked out you can use needs
just as you can for other objects:
Clue here:
};
const needs = {
user(changes) {
loggedIn = true;
userChanges = changes;
},
pretender(fn) {
addPretenderCallback(name, fn);
},
site(changes) {
siteChanges = changes;
},
settings(changes) {
settingChanges = changes;
},
mobileView() {
mobileView = true;
},
};
Final code:
needs.site(cloneJSON(siteFixtures["country_codes.json"]));
3 Likes
system
(system)
Closed
January 27, 2023, 9:58pm
4
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.