testing.ts 831 B

123456789101112131415161718192021222324252627282930313233
  1. import { Container, Service } from "./main";
  2. /**
  3. * A container that can be used for writing tests, contains additional methods
  4. * for binding suitable for writing tests. (see `bindMock`).
  5. */
  6. export class TestContainer extends Container {
  7. /**
  8. * Binds a mock service to the container.
  9. *
  10. * @param service
  11. * @param mock
  12. */
  13. public bindMock<
  14. T extends typeof Service<any> & { ID: string },
  15. U extends Partial<InstanceType<T>>
  16. >(service: T, mock: U): U {
  17. if (this.boundMap.has(service.ID)) {
  18. throw new Error(`Service '${service.ID}' already bound to container. Did you already call bindMock on this ?`)
  19. }
  20. this.boundMap.set(service.ID, mock as any)
  21. this.event$.next({
  22. type: "SERVICE_BIND",
  23. boundeeID: service.ID,
  24. bounderID: undefined,
  25. })
  26. return mock
  27. }
  28. }