I am trying to write a test to see that the result returned from a function I am testing, is one of the correct results.
The result is an array of objects, for example
[{ op: 'replace', path: '', value: { 0: 1 } }]
I want to see if this result is one of the correct answers, the test should pass.
I tried changing one of the tests as mentioned here, but I can't seem to get it to work.
Here is a code snippet:
test(`For arrays, if data gets added, replace operation is sent`, () => {
const data = []
const update = [1]
const result = functionToTest(data, update)
const correct = [{ op: 'replace', path: '', value: { 0: 1 } }]
const correct2 = [{ op: 'replace', path: '', value: [1] }]
//the result should include one of there answers. So if correct or correct 2
//matches, it should pass.
//here I add all the correct answers to an array, to its essensially an
//array of arrays
// [
// [{ op: 'replace', path: '', value: { 0: 1 } }],
// [{ op: 'replace', path: '', value: [1] }]
// ]`
const correctResults = [correct, correct2]
//the result of the function should be contained in one of the correct results
expect(result).toEqual(
expect.arrayContaining(correctResults)
)
})
I will write a code sandbox or something to test it in the browser. Here is the repo for now: https://github.com/jpbnetley/test-json-patch/tree/bugfix/update-tests