Hi guys,
Thank you for this awesome library.
I was wondering how I can write a test case that involves ndjsonStream.
Taking the example from the docs and modifying it a bit:
import ndjsonStream from "can-ndjson-stream";
var users = []
var getUsers = ()=>{ return fetch("/users") }
var fetchUsersList = ()=>{
getUsers()
.then( ( response ) => {
return ndjsonStream( response.body ); //ndjsonStream parses the response.body
} ).then( ( exampleStream ) => {
const reader = exampleStream.getReader();
let read;
reader.read().then( read = ( result ) => {
if ( result.done ) {
return;
}
users.push(result.value) // Each json gets pushed to items array
reader.read().then( read );
} );
} );
}
The code above calls getUsers that returns a Promise object. This Promise is then handled by ndjsonStream. Each JSON read by ndjsonStream is then pushed to the users array.
Now, I want to write a test case using jest that creates a mock function for getUsers and then checks whether the users list gets populated or not. Something like this:
let fakeUsersList = '{"id":"abcd","title":"Random User 1"}\n{"id":"pqrs","title":"Random User 2"}'
import 'isomorphic-fetch'
getUsers = jest.fn(()=>{
return new Promise((resolve, reject)=>{
resolve(new Response(fakeUsersList, {
status: 200,
statusText: 'OK,
headers: {
'Content-type': 'application/x-ndjson'
}
})
)
})
})
it('should populate the user list',()=>{
fetchUsersList()
expect(users.length).toBe(2)
})
The above code basically says, when fetchUsersList is called, instead of calling the actual getUsers, call the dummy function that responds back with a Promise object. The fake Promise object resolves with a fakeUsersList. Then we test whether the users list has been populated or not
The problem I am facing is this:
I was able to create a dummy response for my API call using a Promise object and using isomorphic-fetch to create Response object.
But, I have no way to create a dummy ReadableStream object. When I run my test case, I get the error ReadableStream is not defined, when this line gets executed
ndjsonStream( response.body)
Is there a way to create a mock/dummy of either the ndjsonStream function itself or the ReadableStream object?
Thanks in advance for any help/suggestions that you guys could provide
Hi guys,
Thank you for this awesome library.
I was wondering how I can write a test case that involves
ndjsonStream.Taking the example from the docs and modifying it a bit:
The code above calls
getUsersthat returns a Promise object. This Promise is then handled byndjsonStream. Each JSON read byndjsonStreamis then pushed to theusersarray.Now, I want to write a test case using
jestthat creates a mock function forgetUsersand then checks whether theuserslist gets populated or not. Something like this:The above code basically says, when
fetchUsersListis called, instead of calling the actualgetUsers, call the dummy function that responds back with aPromiseobject. The fake Promise object resolves with afakeUsersList. Then we test whether the users list has been populated or notThe problem I am facing is this:
I was able to create a dummy response for my API call using a Promise object and using
isomorphic-fetchto createResponseobject.But, I have no way to create a dummy
ReadableStreamobject. When I run my test case, I get the errorReadableStream is not defined, when this line gets executedndjsonStream( response.body)Is there a way to create a mock/dummy of either the
ndjsonStreamfunction itself or theReadableStreamobject?Thanks in advance for any help/suggestions that you guys could provide