-
Notifications
You must be signed in to change notification settings - Fork 114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rewrite answer.js into enketoHelper.ts #1063
Rewrite answer.js into enketoHelper.ts #1063
Conversation
…to expand-enketo-helper
moving the methods form answer.js into enketoHelper as a part of the services migration
When testing, I was getting an error from Jest about duplicate modules, one of which was in platforms. This change resolves that error
I have discovered an issue here, when I try to add an activity to a trip or a place in the emulator, that activity does not show up in a list on the card as expected. Hopefully I can find the issue and resolve in the re-write and test process. |
additional test for filterByNameAndVersion fake answers have been constructed to be filtered
completely remove answer.js and all references to it, replace references with references to enketoHelper.ts
One of the things that's popping up with testing, is the need for a fake survey. I had been copying part of the document from somewhere like |
resolveTimestamps is a helper function to saveResponse, but still contains a fair amount of its own logic. Testing the edge cases for this function ensures that it will behave as expected within the larger context
I was able to clear this for the test I was working on in that moment by just copying a part of the survey from the logs. Now, I'm facing an issue with loading the config, in the process of trying to test |
adding a basic test for _loadLazyConfig, in order to ensure that my mock setup for that works, before moving into testing functions that depend on it
I did this by mocking |
I've moved on try trying to test |
currently struggling with i18next and MessageFormat, as I can't get either of those mocked and working
I have not found a way to fix this yet. I did decide to check on On I also checked out JGreenlee:rewrite-services-sept2023, since those changes are in this branch too. Not saving between logins is observed here, but when I input activities they show up, so there are no regressions between To summarize:
I can investigate further tomorrow, but I have a better idea now of when the regressions took place. |
the message format plugin moved! https://github.com/messageformat/messageformat/tree/main/packages/core
After talking, we think this is actually the expected behavior. The log out button should clear everything. If we do not "force sync" before logging out, any unsynced data is lost. This is why we have the "Are you sure?" popup explaining this to users before they log out |
Some updates after meeting with @JGreenlee this morning to discuss the problems I've been having: Instead of trying to mock Next steps:
|
setting __DEV__ to false in globals, so that it can be used throught the testing suit calling the i18n setup in the tests should work once we incorporate the React testing changes
remove old i18n code, update types, comment out broken tests
from log statements, these answers have data and metadata, no labels
ensured that tests still pass
with the changes from e-mission#1049, we are now able to test using i18n, no need to mock!
introduced i18next for the tests updated config in mock, and test of loading config to be accurate to what is expected (missing some '{' ) adjust formatting of function indentation
now that I understand how this function works, I got the xml (filled and unfilled) directly from console.log statements. The tests are now accurate, and cover each of the cases.
I just learned something helpful for testing -- there were a couple of universal packages that were throwing errors in the tests. When I moused over them, they described, for example, that
It was a quick and easy fix (which lots of testing configuration has not been) so documenting for future reference. |
testing for saving the response, both when it works and when the timestamps are invalid, resulting in an error
global.URL = require('url').URL; | ||
global.Blob = require('node:buffer').Blob; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm curious about this - why was it needed?
Do Node environments not have these as globals but browser environments do?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I remember getting an error about this, I don't remember exactly what the error was, but I found that the best way to fix it was to set it like this. comment about this. I think the issue had something to do with the testing environemnt
www/js/survey/enketo/enketoHelper.ts
Outdated
/** @type {EnketoSurveyConfig} _config */ | ||
let _config: EnketoSurveyConfig; | ||
|
||
/** | ||
* _lazyLoadConfig load enketo survey config. If already loaded, return the cached config | ||
* @returns {Promise<EnketoSurveyConfig>} enketo survey config | ||
*/ | ||
export function _lazyLoadConfig() { | ||
if (_config !== undefined) { | ||
return Promise.resolve(_config); | ||
} | ||
return getConfig().then((newConfig) => { | ||
logInfo('Resolved UI_CONFIG_READY promise in enketoHelper, filling in templates'); | ||
_config = newConfig.survey_info.surveys; | ||
return _config; | ||
}); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After analyzing a bit further, I think this function is pointless.
getConfig()
is already cached/lazyloaded, so we could just call it directly. The only difference is that getConfig()
returns the whole config obj while this function only returns the survey_info.surveys
sub-field of the config obj.
I think it would be clearer to remove this function. Anywhere it was used, call getConfig()
and access the subfields from there.
Then move your EnketoSurveyConfig
type def to js/types/appConfigTypes.ts
(it doesn't exist yet on this branch but I started it on your other PR #1098)
The reasoning is that eventually we're going to want a single source of truth for the entire config – not scattered typings for each sub-field of the config.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made those changes in d123b4f, including moving the type. I did not work the type into the existing appConfig type yet though
we also want to test that the timestamps follow the "minute" accuracy convention we implemented, so that if the survey and timelineEntry match within the minute, we use the timelineEntry timestamps, else use the timestamps from the survey e-mission#1063 (comment)
this form is invalide because of the start and end times mismatching
the function was not all that necessary, so I removed it Also moved my survey config type into the appropriate place e-mission#1063 (comment)
adding a response that should get filtered out because the version is too low e-mission#1063 (comment)
Because of the new parameter, I was able to add more test cases to resolve Labels I needed to also clear the locally stored config out of dynamicConfig.ts
//used test multiple configs, not used outside of test | ||
export const resetStoredConfig = function () { | ||
storedConfig = null; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's kind of a workaround, but ok for now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: you may want to prefix with _test_
or something similar to highlight that it is an internal function to be used for testing only
from @JGreenlee's suggestion in review Co-authored-by: Jack Greenlee <[email protected]>
not all (in fact many) of the tests don't need this config at all, so the parameter should be optional to clean things up Co-authored-by: Jack Greenlee <[email protected]>
follow-on to making the config an optional parameter to mockBEMUserCache Instead of passing it in EVERY TIME, it is now a fallback, and the config only needs to be specified and re-specified in the enketoHelper tests added fallback and removed specification from tests that didn't need it
-add types for the parameters of resolveTimestamps -update doc of this function -cast 'timelineEntry' to trip or place where needed -add a few fields that were missing from type defs of ConfirmedPlace and EnketoSurveyConfig
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
if the survey doesn't use start and end times, timestamps will be null. This is fine and we can use optional chaining to prevent throwing an error here
…s/e-mission-phone into expand-enketo-helper
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## service_rewrite_2023 #1063 +/- ##
========================================================
+ Coverage 52.67% 56.44% +3.77%
========================================================
Files 21 22 +1
Lines 1198 1311 +113
Branches 266 296 +30
========================================================
+ Hits 631 740 +109
- Misses 567 571 +4
Flags with carried forward coverage won't be shown. Click here to find out more.
|
This is a long story, probably best summarized in e-mission/e-mission-docs#640
I noticed this in the dependencies. Exciting! @Abby-Wheelis in the next round of intake form improvements, you may be able to allow participants to specify the XML file directly, either in the form (not sure if upload is supported), or by adding to the issue in a new comment or by adding to the PR directly. That should cut down significantly on the amount of busy work you have to do! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am going to merge this now so I can get a release out to staging, but please fix the issues outlined here in a future fix:
- comments from me
- codecoverage results (thanks to Jiji!!) about the lines that are not covered with the current tests.
//used test multiple configs, not used outside of test | ||
export const resetStoredConfig = function () { | ||
storedConfig = null; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: you may want to prefix with _test_
or something similar to highlight that it is an internal function to be used for testing only
); | ||
} | ||
|
||
export async function fetchSurvey(url: string) { | ||
const responseText = await fetchUrlCached(url); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this the right thing to do? Is it possible for the cache to be out of date - for example, right after the user has edited the survey.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function fetches the survey, not the response (ie this is what is handling the xml/json differences) that @JGreenlee added. Our previous response function [loadPreviousResponseForSurvey
] does re-fetch every time.
expect(getInstanceStr(null, opts)).toBe(null); | ||
//if there is a prefilled survey, return it | ||
expect(getInstanceStr(xmlModel, opts)).toBe(filled); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't you also check with an xmlResponse that does not have the start/end datetime?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, one that is already filled, but does not have the timestamps filled?
I'm a little confused what you mean, but maybe as I comb through the codecov report I can figure out where the gap you're referring to is.
This PR is based on Jack's rewrite PR so that I can work off his mocks and hopefully minimize merge conflicts once that branch can be merged.
Addresses one of the rewrites in: ✍ Angular Services needing rewrite