Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

messed up args in setInterval / setTimeout in fakeAsyncZones #1032

Closed
e-hein opened this issue Feb 27, 2018 · 3 comments · Fixed by #1033
Closed

messed up args in setInterval / setTimeout in fakeAsyncZones #1032

e-hein opened this issue Feb 27, 2018 · 3 comments · Fixed by #1033

Comments

@e-hein
Copy link

e-hein commented Feb 27, 2018

this._setInterval(task.invoke, task.data['delay'], (task.data as any)['args']);

The genuine args-Array in a setInterval or setTimeout call contains the function to be called and the delay. The arguments for the function start at the third position. By passing the arguments like this, the function invoked will get an array as argument that contains the function itself and the delay and the expected needed arguments.

Sample:
setInterval((name) => console.log('my name:', name), 100, 'Emanuel')
expected call:
('Emanuel') => console.log('my name:', 'Emanuel')
actual call:
([function, 100, 'Emanuel') => console.log('my name:', [function, 100, 'Emanuel'])

@JiaLiPassion
Copy link
Collaborator

@e-hein , thank you for posting the issue, this is a known issue, I will fix it.

@e-hein
Copy link
Author

e-hein commented Feb 27, 2018

spec:

it('should pass arguments to times', () => {
  fakeAsyncTestZone.run(() => {
    let value = 'genuine value';
    let id = setTimeout((arg1, arg2) => {
      value = arg1 + arg2;
    }, 0, 'expected', ' value');

    testZoneSpec.tick();
    expect(value).toBe('expected value');
  });
});

it('should pass arguments to periodic timers', () => {
  fakeAsyncTestZone.run(() => {
    let value = 'genuine value';
    let id = setInterval((arg1, arg2) => {
      value = arg1 + arg2;
    }, 10, 'expected', ' value');

    testZoneSpec.tick(10);
    expect(value).toBe('expected value');
  })
});

fix:

        case 'setTimeout':
          task.data['handleId'] =
              this._setTimeout(task.invoke, task.data['delay'], Array.prototype.slice.call((task.data as { args: IArguments }).args, 2));
          break;
        case 'setInterval':
          task.data['handleId'] =
              this._setInterval(task.invoke, task.data['delay'], ...Array.prototype.slice.call((task.data as { args: IArguments }).args, 2));

@JiaLiPassion
Copy link
Collaborator

@e-hein , thank you for the fix and the spec.

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

Successfully merging a pull request may close this issue.

2 participants