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

Call a Callback Multiple Times #397

Closed
13 tasks
shavyg2 opened this issue Dec 5, 2018 · 3 comments
Closed
13 tasks

Call a Callback Multiple Times #397

shavyg2 opened this issue Dec 5, 2018 · 3 comments

Comments

@shavyg2
Copy link

shavyg2 commented Dec 5, 2018

Description

I want to simulate a callback that is fired more than once.
This of a upload function.

I have a

let progress = {loaded:0.5,total:1}

Issue

The issue is that i can't fire a callback more than once different arguments. Or atleast i don't know how to.

Environment

  • node -v output:
    v10.11.0
  • npm -v (or yarn --version) output:
    1.12.3
  • npm ls testdouble (or yarn list testdouble) version:
    yarn list v1.12.3
    warning Filtering by arguments is deprecated. Please use the pattern option instead.
    └─ testdouble@3.9.1
    Done in 1.56s.

Failing Test

  • Fork the repo
  • Add a failing test (probably to the `/regression/src' directory)
  • Submit a pull request for the failing test or link to your branch here

Example Repo

  • Create a minimal repository that reproduces the issue
  • Make sure that a fresh clone can run only npm it and observe the issue
  • Link to that repo here

Runkit Notebook

  • Create a Runkit notebook
  • Invoke var td = require('testdouble') at the top
  • Verify the behavior your issue is concerned with by clicking "Run"
  • Link to the Runkit here

Code-fenced Examples

var td = require('testdouble')

// Your steps here.
const progress = td.func();
const myCallback = td.func();
td.when(progress(td.callback)).thenCallback({loaded:0.5,total:1})

//can i call it again?

progress(myCallback)


//assert myCallback called more than once.
@searls
Copy link
Member

searls commented Dec 5, 2018

You could specify that any number of stubbings only fire 1 time. Once satisfied they'll be effectively disabled. Because the last-stubbing-in is the first to be satisfied by an invocation, you'll need to stub them in reverse order as shown below

// Subsequent calls
td.when(progress(td.callback)).thenCallback({loaded:0.8,total:3})
// Second call
td.when(progress(td.callback), {times: 1}).thenCallback({loaded:0.6,total:2})
// First call
td.when(progress(td.callback), {times: 1}).thenCallback({loaded:0.5,total:1})

Alternatively you can look at using thenDo for more freedom in creating custom or conditional behavior

@searls searls closed this as completed Dec 5, 2018
@shavyg2
Copy link
Author

shavyg2 commented Dec 5, 2018

I may not have explained myself well enough.

I want the myCallback to be called multiple time to simulate.
https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequestEventTarget/onprogress

The issue is that I am not testing one function at a time i am testing 2.

var td = require('testdouble')

// Your steps here.
const progress = td.func();
const myCallback = td.func();
td.when(progress(td.callback)).thenCallback({loaded:0.5,total:1})


progress(myCallback)


// should essentially do

progress() 1 call
myCallback() 1 call
myCallback() 2 call
myCallback() 3 call



//essentially if i have an options like, not saying i 
//need it just trying to figure out the work around to get it.

const progress = td.func();
const myCallback = td.func();
td.when(progress(td.callback))
    .thenCallback({loaded:0.2,total:1})
    .thenCallback({loaded:0.5,total:1})
    .thenCallback({loaded:0.7,total:1})
    .thenCallback({loaded:1,total:1})


//internally looks like
progress((callback)=>{
    callback({loaded:0.2,total:1})
    callback({loaded:0.5,total:1})
    callback({loaded:0.8,total:1})
})

@searls
Copy link
Member

searls commented Dec 5, 2018

For this kind of nesting dolls scenario, I think you're going to have to write a custom function in thenDo() that calls the callback multiple times.

const progress = td.func()
td.when(progress(td.matchers.isA(Function))).thenDo(cb => {
  cb({loaded:0.2,total:1})
  cb({loaded:0.5,total:1})
  cb({loaded:0.7,total:1})
  cb({loaded:1,total:1})
})

progress(console.log) /*
{ loaded: 0.2, total: 1 }
{ loaded: 0.5, total: 1 }
{ loaded: 0.7, total: 1 }
{ loaded: 1, total: 1 }
*/

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

No branches or pull requests

2 participants