Skip to content
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

uiRecordApi is missing implementation of utility functions #79

Closed
hagai-shatz opened this issue Jul 24, 2019 · 2 comments · Fixed by #83
Closed

uiRecordApi is missing implementation of utility functions #79

hagai-shatz opened this issue Jul 24, 2019 · 2 comments · Fixed by #83

Comments

@hagai-shatz
Copy link

It is impossible to test code that uses getFieldValue from uiRecordApi without reimplementing the function.
The mock for uiRecordApi in lwc-recipes includes the implementation of getFieldValue:
https://github.com/trailheadapps/lwc-recipes/blob/master/force-app/test/jest-mocks/lightning/uiRecordApi.js

It would make sense to have the same implementation provided here.

@trevor-bliss
Copy link
Contributor

Hi @hagai-shatz. Thanks for the Issue. In general, I don't think it's a good idea to add the actual implementation for these methods because they can differ from the real code or get out of date over time. The lwc-recipe implementation is a good example of this. The real getFieldValue doesn't account for a results prop on the first param, and returns undefined instead of null if no value is found.

I guess for this one we can make an exception though since I think the functionality is unlikely to change much and it is a pain to have to copy that implementation whenever you use it. I'm going to modify what is in the lwc-recipe repo though so more closely represent what the real function does.

@hagai-shatz
Copy link
Author

Hi @trevor-bliss
I totally agree that services should be mocked, but it is almost impossible to test your code if utility functions like getFieldValue and getFieldDisplayValue are not implemented.
We took the code from lwc-recipe into our project to run tests and as you mentioned, it didn't work as expected, not just the data.result support, but also no support for string path.

This is the implementation we have now:

	if (data) {
		const fields = fieldReference.fieldApiName ?
			[fieldReference.objectApiName].concat(fieldReference.fieldApiName.split('.')) :
			fieldReference.split('.');
		let record = data.result || data;
		if (record.apiName === fields.shift()) {
			while (record && record.fields && fields.length) {
				record = record.fields[fields.shift()];
			}
			return record && record.value;
		}
		return null;
	}

Following your comment, I will probably remove the support for data.result and return undefined; at the end.

Notice that we implemented similar mocking for @salesfroce/apex/getSObjectValue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants