Is there an update on this emulator, or any other way of creating ObjectSet collections for unit tests?
I want to mock an ObjectSet collection of our custom object type. I’ve made these attempts with the corresponding errors:
// Type 'null' is not assignable to type 'ObjectSet'.
const mockObjects: ObjectSet<CustomObject> = null;
and
// Type 'never[]' is missing the following properties from type 'ObjectSet': searchAround, orderBy, orderByRelevance, all, and 12 more.
const mockObjects: ObjectSet<CustomObject> = [];
and
// Type '() => void' is not assignable to type
// '<TResult extends FunctionsApi.OntologyObject>(propertySelector:
// (object: CustomObject) => SearchAroundObjectSetProvider<TResult>)
// => ObjectSet<...>'.
const mockObjects: ObjectSet<CustomObject> = {
searchAround: () => {},
orderBy: () => {},
orderByRelevance: () => {},
all: () => {},
};
We have a function that finds all objects of customProperty “my_type” with a specific id and performs modifications on the items:
@OntologyEditFunction()
public async modifyObject(id: string): Promise<void> {
const listOfObjects = Objects.search().customObject().filter(
(anObject) => Filters.and(anObject.id.exactMatch(id),anObject.customProperty.exactMatch('my_type'))
).all();
// Perform modifications on listOfObjects as needed
}
We want to mock the “api / database call” Objects.search().customObject() to return a hardcoded mock object defined in our test
const mockObjects = [
{
id: 'test-id-456',
customProperty: 'my_type',
}
];
const mockSearch = {
customObject(): jest.fn().mockReturnValue(mockObjects)
};
Running the test with Jest gives this error, since the id type is ‘string’:
TypeError: anObject.id.exactMatch is not a function
TypeError: anObject.customProperty.exactMatch is not a function
I tried to alleviate this by changing the mock object to have a similar structure as the filter expects:
id: {
id: 'test-id-456', // not sure if this matches?
exactMatch: (test) => test === 'test-id-456'
},
customProperty: {
customProperty: 'my_type', // not sure if this matches?
exactMatch: (test) => test === 'my_type'
},
Running the test with Jest gives this error
TypeError: ontology_api_1.Objects.search(...).customObject(...).filter(...).all is not a function
Overall, I’m hoping for something like an Object Literal:
const mockObjects = [
{
id: 'test-id-456',
customProperty: 'my_type',
}
];
or a class with getters / setters:
// HashMap in Java:
ObjectSet<CustomObject> customObjects = new ObjectSet<CustomObject>();
customObjects.put({
id: 'test-id-456',
customProperty: 'my_type',
});
Thank you!