pubkey / rxdb

A fast, local first, reactive Database for JavaScript Applications https://rxdb.info/
https://rxdb.info/
Apache License 2.0
20.96k stars 1.02k forks source link

Export database related classes #41

Closed thani-sh closed 7 years ago

thani-sh commented 7 years ago

Hi, Can you export RxDatabase, RxCollection, RxDocument, RxQuery and other classes so they can be extended. It'll also be useful when we need to create mocks for tests.

pubkey commented 7 years ago

@mnmtanish I dont know if this is the right thing to do. Could you describe a use-case where you need to extend the Classes?

thani-sh commented 7 years ago

As an example, let's say I want to unit test this class with jasmine.

class MyClass(collection: RxCollection) {
  // ...
}

const coll = await db.collection('name')
const myClass = new MyClass(coll)

At the moment, I'm using some wild type assertions to use mock versions of RxDB related objects.

class MockRxCollection {
  public findSpy = spyOn(this, 'find')
  public find(queryObj?: any): RxQuery {
    return null
  }

  public insertSpy = spyOn(this, 'insert')
  public insert(json: any): Promise<RxDocument> {
    return Promise.resolve(null)
  }
}

const coll = new MockRxCollection() as any as RxCollection
const myClass = new MyClass(coll)

It'll be much easier to do this if I can extend the RxCollection class. It's also possible to write a tool to automatically mock all methods in a class.

class MockRxCollection extends RxCollection {
  public findSpy = spyOn(this, 'find')
  public insertSpy = spyOn(this, 'insert')
}

const coll = new MockRxCollection()
const myClass = new MyClass(coll)
pubkey commented 7 years ago

I don't think that it's a good practice when every npm-modules exposes/leaky-abstracts, its internal classes just to make them easier to spy in tests from the outside.

Isn't it possible to spy on the instance instead of the class? How do you do such tests with other modules?