Skip to content

v4 planning

Chuanqi Sun edited this page Mar 19, 2019 · 1 revision

Capabilities

  1. infinite de-reference: mockObject.prop.func().prop.func().prop.func().prop still returns a mock object.
  2. decouple from jasmine: replace spy with a built-in journal that is test framework agnostic
  3. ergonomic interface:
    1. consumer interface is identical to original object that is being mocked.
    2. admin interface is a one-time entry, e.g. mockObject._admin.prop1.func().prop2 = 42 would result in mockOjbect.prop1.func().prop2 to actually return 42, but also leaves a journal on all the get and apply actions along the chain.

Proof of concept

const stubObject = Object.create(null);
const handler = {
    get: function(obj, prop) {
      console.log('get');
      if (!(prop in stubObject)) {
        stubObject[prop] = new Proxy(new Function(), handler);
      }
      return stubObject[prop];
    },
    apply: function(target, thisArg, argumentList) {
      console.log('apply');
      return new Proxy(Object.create(null), handler);
    },
    set: function(obj, prop, value) {
      console.log('set');
      stubObject[prop] = value;
    }
};

var p = new Proxy({}, handler);
Clone this wiki locally