service-container.spec.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. import {expect} from 'chai';
  2. import {Service} from './service.js';
  3. import {format} from '@e22m4u/js-format';
  4. import {ServiceContainer} from './service-container.js';
  5. describe('ServiceContainer', function () {
  6. describe('get', function () {
  7. it('throws an error if no constructor given', function () {
  8. const container = new ServiceContainer();
  9. const throwable = v => () => container.get(v);
  10. const error = v =>
  11. format(
  12. 'The first argument of ServicesContainer.get must be ' +
  13. 'a class constructor, but %s given.',
  14. v,
  15. );
  16. expect(throwable('str')).to.throw(error('"str"'));
  17. expect(throwable(10)).to.throw(error('10'));
  18. expect(throwable(true)).to.throw(error('true'));
  19. expect(throwable(false)).to.throw(error('false'));
  20. expect(throwable(undefined)).to.throw(error('undefined'));
  21. expect(throwable(null)).to.throw(error('null'));
  22. expect(throwable([])).to.throw(error('Array'));
  23. expect(throwable({})).to.throw(error('Object'));
  24. });
  25. describe('Service', function () {
  26. it('passes itself and given arguments to the given constructor', function () {
  27. let executed = 0;
  28. let givenContainer;
  29. let givenArgs;
  30. class MyService extends Service {
  31. constructor(container, ...args) {
  32. super(container);
  33. executed++;
  34. givenContainer = container;
  35. givenArgs = args;
  36. }
  37. }
  38. const container = new ServiceContainer();
  39. const service = container.get(MyService, 'foo', 'bar');
  40. expect(service).to.be.instanceof(MyService);
  41. expect(executed).to.be.eq(1);
  42. expect(givenContainer).to.be.eq(container);
  43. expect(givenArgs).to.be.eql(['foo', 'bar']);
  44. });
  45. it('instantiates from an existing factory function', function () {
  46. let executed = 0;
  47. let givenContainer;
  48. let givenArgs;
  49. class MyService extends Service {
  50. constructor(container, ...args) {
  51. super(container);
  52. executed++;
  53. givenContainer = container;
  54. givenArgs = args;
  55. }
  56. }
  57. const container = new ServiceContainer();
  58. container.add(MyService, 'foo', 'bar');
  59. expect(executed).to.be.eq(0);
  60. const service = container.get(MyService);
  61. expect(service).to.be.instanceof(MyService);
  62. expect(executed).to.be.eq(1);
  63. expect(givenContainer).to.be.eq(container);
  64. expect(givenArgs).to.be.eql(['foo', 'bar']);
  65. });
  66. it('overrides an existing factory function', function () {
  67. let executed = 0;
  68. let givenContainer;
  69. let givenArgs;
  70. class MyService extends Service {
  71. constructor(container, ...args) {
  72. super(container);
  73. executed++;
  74. givenContainer = container;
  75. givenArgs = args;
  76. }
  77. }
  78. const container = new ServiceContainer();
  79. container.add(MyService, 'foo', 'bar');
  80. expect(executed).to.be.eq(0);
  81. const service = container.get(MyService, 'baz', 'qux');
  82. expect(service).to.be.instanceof(MyService);
  83. expect(executed).to.be.eq(1);
  84. expect(givenContainer).to.be.eq(container);
  85. expect(givenArgs).to.be.eql(['baz', 'qux']);
  86. });
  87. it('caches a new instance', function () {
  88. let executed = 0;
  89. class MyService extends Service {
  90. constructor(container) {
  91. super(container);
  92. ++executed;
  93. }
  94. }
  95. const container = new ServiceContainer();
  96. const service1 = container.get(MyService);
  97. const service2 = container.get(MyService);
  98. expect(service1).to.be.instanceof(MyService);
  99. expect(service2).to.be.instanceof(MyService);
  100. expect(service1).to.be.eq(service2);
  101. expect(executed).to.be.eq(1);
  102. });
  103. it('overrides the cached instance', function () {
  104. let executed = 0;
  105. const givenArgs = [];
  106. class MyService extends Service {
  107. constructor(container, arg) {
  108. super(container);
  109. ++executed;
  110. givenArgs.push(arg);
  111. }
  112. }
  113. const container = new ServiceContainer();
  114. const service1 = container.get(MyService, 'foo');
  115. const service2 = container.get(MyService);
  116. const service3 = container.get(MyService, 'bar');
  117. const service4 = container.get(MyService);
  118. expect(service1).to.be.instanceof(MyService);
  119. expect(service2).to.be.instanceof(MyService);
  120. expect(service3).to.be.instanceof(MyService);
  121. expect(service4).to.be.instanceof(MyService);
  122. expect(service1).to.be.eq(service2);
  123. expect(service2).to.be.not.eq(service3);
  124. expect(service3).to.be.eq(service4);
  125. expect(executed).to.be.eq(2);
  126. expect(givenArgs).to.be.eql(['foo', 'bar']);
  127. });
  128. });
  129. describe('non-Service', function () {
  130. it('passes given arguments to the given constructor', function () {
  131. let executed = 0;
  132. let givenArgs;
  133. class MyService {
  134. constructor(...args) {
  135. executed++;
  136. givenArgs = args;
  137. }
  138. }
  139. const container = new ServiceContainer();
  140. const service = container.get(MyService, 'foo', 'bar');
  141. expect(service).to.be.instanceof(MyService);
  142. expect(executed).to.be.eq(1);
  143. expect(givenArgs).to.be.eql(['foo', 'bar']);
  144. });
  145. it('instantiates from an existing factory function', function () {
  146. let executed = 0;
  147. let givenArgs;
  148. class MyService {
  149. constructor(...args) {
  150. executed++;
  151. givenArgs = args;
  152. }
  153. }
  154. const container = new ServiceContainer();
  155. container.add(MyService, 'foo', 'bar');
  156. expect(executed).to.be.eq(0);
  157. const service = container.get(MyService);
  158. expect(service).to.be.instanceof(MyService);
  159. expect(executed).to.be.eq(1);
  160. expect(givenArgs).to.be.eql(['foo', 'bar']);
  161. });
  162. it('overrides an existing factory function', function () {
  163. let executed = 0;
  164. let givenArgs;
  165. class MyService {
  166. constructor(...args) {
  167. executed++;
  168. givenArgs = args;
  169. }
  170. }
  171. const container = new ServiceContainer();
  172. container.add(MyService, 'foo', 'bar');
  173. expect(executed).to.be.eq(0);
  174. const service = container.get(MyService, 'baz', 'qux');
  175. expect(service).to.be.instanceof(MyService);
  176. expect(executed).to.be.eq(1);
  177. expect(givenArgs).to.be.eql(['baz', 'qux']);
  178. });
  179. it('caches a new instance', function () {
  180. let executed = 0;
  181. class MyService {
  182. constructor() {
  183. ++executed;
  184. }
  185. }
  186. const container = new ServiceContainer();
  187. const service1 = container.get(MyService);
  188. const service2 = container.get(MyService);
  189. expect(service1).to.be.instanceof(MyService);
  190. expect(service2).to.be.instanceof(MyService);
  191. expect(service1).to.be.eq(service2);
  192. expect(executed).to.be.eq(1);
  193. });
  194. it('overrides the cached instance', function () {
  195. let executed = 0;
  196. const givenArgs = [];
  197. class MyService {
  198. constructor(arg) {
  199. ++executed;
  200. givenArgs.push(arg);
  201. }
  202. }
  203. const container = new ServiceContainer();
  204. const service1 = container.get(MyService, 'foo');
  205. const service2 = container.get(MyService);
  206. const service3 = container.get(MyService, 'bar');
  207. const service4 = container.get(MyService);
  208. expect(service1).to.be.instanceof(MyService);
  209. expect(service2).to.be.instanceof(MyService);
  210. expect(service3).to.be.instanceof(MyService);
  211. expect(service4).to.be.instanceof(MyService);
  212. expect(service1).to.be.eq(service2);
  213. expect(service2).to.be.not.eq(service3);
  214. expect(service3).to.be.eq(service4);
  215. expect(executed).to.be.eq(2);
  216. expect(givenArgs).to.be.eql(['foo', 'bar']);
  217. });
  218. });
  219. });
  220. describe('has', function () {
  221. describe('Service', function () {
  222. it('returns true when a given constructor has its cached instance or false', function () {
  223. const container = new ServiceContainer();
  224. class MyService extends Service {}
  225. expect(container.has(MyService)).to.be.false;
  226. container.get(MyService);
  227. expect(container.has(MyService)).to.be.true;
  228. });
  229. it('returns true when a given constructor has its factory function or false', function () {
  230. const container = new ServiceContainer();
  231. class MyService extends Service {}
  232. expect(container.has(MyService)).to.be.false;
  233. container.add(MyService);
  234. expect(container.has(MyService)).to.be.true;
  235. });
  236. });
  237. describe('non-Service', function () {
  238. it('returns true when a given constructor has its cached instance or false', function () {
  239. const container = new ServiceContainer();
  240. class MyService {}
  241. expect(container.has(MyService)).to.be.false;
  242. container.get(MyService);
  243. expect(container.has(MyService)).to.be.true;
  244. });
  245. it('returns true when a given constructor has its factory function or false', function () {
  246. const container = new ServiceContainer();
  247. class MyService {}
  248. expect(container.has(MyService)).to.be.false;
  249. container.add(MyService);
  250. expect(container.has(MyService)).to.be.true;
  251. });
  252. });
  253. });
  254. describe('add', function () {
  255. it('throws an error if no constructor given', function () {
  256. const container = new ServiceContainer();
  257. const throwable = v => () => container.add(v);
  258. const error = v =>
  259. format(
  260. 'The first argument of ServicesContainer.add must be ' +
  261. 'a class constructor, but %s given.',
  262. v,
  263. );
  264. expect(throwable()).to.throw(error('undefined'));
  265. expect(throwable('str')).to.throw(error('"str"'));
  266. expect(throwable(10)).to.throw(error('10'));
  267. expect(throwable(true)).to.throw(error('true'));
  268. expect(throwable(false)).to.throw(error('false'));
  269. expect(throwable(null)).to.throw(error('null'));
  270. expect(throwable([])).to.throw(error('Array'));
  271. expect(throwable({})).to.throw(error('Object'));
  272. });
  273. describe('Service', function () {
  274. it('returns itself', function () {
  275. class MyService extends Service {}
  276. const container = new ServiceContainer();
  277. const res = container.add(MyService);
  278. expect(res).to.be.eq(container);
  279. });
  280. it('provides given arguments to the factory function', function () {
  281. let executed = 0;
  282. let givenContainer;
  283. let givenArgs;
  284. class MyService extends Service {
  285. constructor(container, ...args) {
  286. super(container);
  287. executed++;
  288. givenContainer = container;
  289. givenArgs = args;
  290. }
  291. }
  292. const container = new ServiceContainer();
  293. container.add(MyService, 'foo', 'bar');
  294. expect(executed).to.be.eq(0);
  295. const service = container.get(MyService);
  296. expect(service).to.be.instanceof(MyService);
  297. expect(executed).to.be.eq(1);
  298. expect(givenContainer).to.be.eq(container);
  299. expect(givenArgs).to.be.eql(['foo', 'bar']);
  300. });
  301. it('overrides a cached instance of the given constructor', function () {
  302. class MyService extends Service {}
  303. const container = new ServiceContainer();
  304. const service1 = container.get(MyService);
  305. const service2 = container.get(MyService);
  306. expect(service1).to.be.eq(service2);
  307. container.add(MyService);
  308. const service3 = container.get(MyService);
  309. const service4 = container.get(MyService);
  310. expect(service3).to.be.eq(service4);
  311. expect(service3).to.be.not.eq(service1);
  312. });
  313. it('overrides constructor arguments of the factory function', function () {
  314. let executed = 0;
  315. let givenContainer;
  316. let givenArgs;
  317. class MyService extends Service {
  318. constructor(container, ...args) {
  319. super(container);
  320. executed++;
  321. givenContainer = container;
  322. givenArgs = args;
  323. }
  324. }
  325. const container = new ServiceContainer();
  326. container.add(MyService, 'foo', 'bar');
  327. expect(executed).to.be.eq(0);
  328. container.add(MyService, 'baz', 'qux');
  329. const service = container.get(MyService);
  330. expect(service).to.be.instanceof(MyService);
  331. expect(executed).to.be.eq(1);
  332. expect(givenContainer).to.be.eq(container);
  333. expect(givenArgs).to.be.eql(['baz', 'qux']);
  334. });
  335. });
  336. describe('non-Service', function () {
  337. it('returns itself', function () {
  338. class MyService {}
  339. const container = new ServiceContainer();
  340. const res = container.add(MyService);
  341. expect(res).to.be.eq(container);
  342. });
  343. it('provides given arguments to the factory function', function () {
  344. let executed = 0;
  345. let givenArgs;
  346. class MyService {
  347. constructor(...args) {
  348. executed++;
  349. givenArgs = args;
  350. }
  351. }
  352. const container = new ServiceContainer();
  353. container.add(MyService, 'foo', 'bar');
  354. expect(executed).to.be.eq(0);
  355. const service = container.get(MyService);
  356. expect(service).to.be.instanceof(MyService);
  357. expect(executed).to.be.eq(1);
  358. expect(givenArgs).to.be.eql(['foo', 'bar']);
  359. });
  360. it('overrides a cached instance of the given constructor', function () {
  361. class MyService {}
  362. const container = new ServiceContainer();
  363. const service1 = container.get(MyService);
  364. const service2 = container.get(MyService);
  365. expect(service1).to.be.eq(service2);
  366. container.add(MyService);
  367. const service3 = container.get(MyService);
  368. const service4 = container.get(MyService);
  369. expect(service3).to.be.eq(service4);
  370. expect(service3).to.be.not.eq(service1);
  371. });
  372. it('overrides constructor arguments of the factory function', function () {
  373. let executed = 0;
  374. let givenArgs;
  375. class MyService {
  376. constructor(...args) {
  377. executed++;
  378. givenArgs = args;
  379. }
  380. }
  381. const container = new ServiceContainer();
  382. container.add(MyService, 'foo', 'bar');
  383. expect(executed).to.be.eq(0);
  384. container.add(MyService, 'baz', 'qux');
  385. const service = container.get(MyService);
  386. expect(service).to.be.instanceof(MyService);
  387. expect(executed).to.be.eq(1);
  388. expect(givenArgs).to.be.eql(['baz', 'qux']);
  389. });
  390. });
  391. });
  392. describe('use', function () {
  393. it('throws an error if no constructor given', function () {
  394. const container = new ServiceContainer();
  395. const throwable = v => () => container.use(v);
  396. const error = v =>
  397. format(
  398. 'The first argument of ServicesContainer.use must be ' +
  399. 'a class constructor, but %s given.',
  400. v,
  401. );
  402. expect(throwable()).to.throw(error('undefined'));
  403. expect(throwable('str')).to.throw(error('"str"'));
  404. expect(throwable(10)).to.throw(error('10'));
  405. expect(throwable(true)).to.throw(error('true'));
  406. expect(throwable(false)).to.throw(error('false'));
  407. expect(throwable(null)).to.throw(error('null'));
  408. expect(throwable([])).to.throw(error('Array'));
  409. expect(throwable({})).to.throw(error('Object'));
  410. });
  411. describe('Service', function () {
  412. it('returns itself', function () {
  413. class MyService extends Service {}
  414. const container = new ServiceContainer();
  415. const res = container.use(MyService);
  416. expect(res).to.be.eq(container);
  417. });
  418. it('passes itself and given arguments to the given constructor', function () {
  419. let executed = 0;
  420. let givenContainer;
  421. let givenArgs;
  422. class MyService extends Service {
  423. constructor(container, ...args) {
  424. super(container);
  425. executed++;
  426. givenContainer = container;
  427. givenArgs = args;
  428. }
  429. }
  430. const container = new ServiceContainer();
  431. container.use(MyService, 'foo', 'bar');
  432. expect(executed).to.be.eq(1);
  433. expect(givenContainer).to.be.eq(container);
  434. expect(givenArgs).to.be.eql(['foo', 'bar']);
  435. });
  436. it('overrides an existing factory function', function () {
  437. let executed = 0;
  438. let givenContainer;
  439. let givenArgs;
  440. class MyService extends Service {
  441. constructor(container, ...args) {
  442. super(container);
  443. executed++;
  444. givenContainer = container;
  445. givenArgs = args;
  446. }
  447. }
  448. const container = new ServiceContainer();
  449. container.add(MyService, 'foo', 'bar');
  450. expect(executed).to.be.eq(0);
  451. container.use(MyService, 'baz', 'qux');
  452. expect(executed).to.be.eq(1);
  453. expect(givenContainer).to.be.eq(container);
  454. expect(givenArgs).to.be.eql(['baz', 'qux']);
  455. });
  456. it('caches a new instance', function () {
  457. let executed = 0;
  458. let service;
  459. class MyService extends Service {
  460. constructor(container) {
  461. super(container);
  462. ++executed;
  463. service = this;
  464. }
  465. }
  466. const container = new ServiceContainer();
  467. container.use(MyService);
  468. const cachedService = container.get(MyService);
  469. expect(cachedService).to.be.instanceof(MyService);
  470. expect(cachedService).to.be.eq(service);
  471. expect(executed).to.be.eq(1);
  472. });
  473. it('overrides the cached instance', function () {
  474. let executed = 0;
  475. let service;
  476. let givenArgs;
  477. class MyService extends Service {
  478. constructor(container, ...args) {
  479. super(container);
  480. ++executed;
  481. service = this;
  482. givenArgs = args;
  483. }
  484. }
  485. const container = new ServiceContainer();
  486. container.use(MyService, 'foo');
  487. expect(executed).to.be.eq(1);
  488. expect(service).to.be.instanceof(MyService);
  489. expect(givenArgs).to.be.eql(['foo']);
  490. const service1 = service;
  491. container.use(MyService, 'bar');
  492. expect(executed).to.be.eq(2);
  493. expect(service).to.be.instanceof(MyService);
  494. expect(givenArgs).to.be.eql(['bar']);
  495. const service2 = service;
  496. expect(service2).to.be.not.eq(service1);
  497. });
  498. });
  499. describe('non-Service', function () {
  500. it('returns itself', function () {
  501. class MyService {}
  502. const container = new ServiceContainer();
  503. const res = container.use(MyService);
  504. expect(res).to.be.eq(container);
  505. });
  506. it('passes given arguments to the given constructor', function () {
  507. let executed = 0;
  508. let givenArgs;
  509. class MyService {
  510. constructor(...args) {
  511. executed++;
  512. givenArgs = args;
  513. }
  514. }
  515. const container = new ServiceContainer();
  516. container.use(MyService, 'foo', 'bar');
  517. expect(executed).to.be.eq(1);
  518. expect(givenArgs).to.be.eql(['foo', 'bar']);
  519. });
  520. it('overrides an existing factory function', function () {
  521. let executed = 0;
  522. let givenArgs;
  523. class MyService {
  524. constructor(...args) {
  525. executed++;
  526. givenArgs = args;
  527. }
  528. }
  529. const container = new ServiceContainer();
  530. container.add(MyService, 'foo', 'bar');
  531. expect(executed).to.be.eq(0);
  532. container.use(MyService, 'baz', 'qux');
  533. expect(executed).to.be.eq(1);
  534. expect(givenArgs).to.be.eql(['baz', 'qux']);
  535. });
  536. it('caches a new instance', function () {
  537. let executed = 0;
  538. let service;
  539. class MyService {
  540. constructor() {
  541. ++executed;
  542. service = this;
  543. }
  544. }
  545. const container = new ServiceContainer();
  546. container.use(MyService);
  547. const cachedService = container.get(MyService);
  548. expect(cachedService).to.be.instanceof(MyService);
  549. expect(cachedService).to.be.eq(service);
  550. expect(executed).to.be.eq(1);
  551. });
  552. it('overrides the cached instance', function () {
  553. let executed = 0;
  554. let service;
  555. let givenArgs;
  556. class MyService {
  557. constructor(...args) {
  558. ++executed;
  559. service = this;
  560. givenArgs = args;
  561. }
  562. }
  563. const container = new ServiceContainer();
  564. container.use(MyService, 'foo');
  565. expect(executed).to.be.eq(1);
  566. expect(service).to.be.instanceof(MyService);
  567. expect(givenArgs).to.be.eql(['foo']);
  568. const service1 = service;
  569. container.use(MyService, 'bar');
  570. expect(executed).to.be.eq(2);
  571. expect(service).to.be.instanceof(MyService);
  572. expect(givenArgs).to.be.eql(['bar']);
  573. const service2 = service;
  574. expect(service2).to.be.not.eq(service1);
  575. });
  576. });
  577. });
  578. describe('set', function () {
  579. it('requires the "ctor" argument to be a class constructor', function () {
  580. const container = new ServiceContainer();
  581. const throwable = v => () => container.set(v, {});
  582. const error = v =>
  583. format(
  584. 'The first argument of ServicesContainer.set must be ' +
  585. 'a class constructor, but %s given.',
  586. v,
  587. );
  588. expect(throwable()).to.throw(error('undefined'));
  589. expect(throwable('str')).to.throw(error('"str"'));
  590. expect(throwable(10)).to.throw(error('10'));
  591. expect(throwable(true)).to.throw(error('true'));
  592. expect(throwable(false)).to.throw(error('false'));
  593. expect(throwable(null)).to.throw(error('null'));
  594. expect(throwable([])).to.throw(error('Array'));
  595. expect(throwable({})).to.throw(error('Object'));
  596. throwable(String)();
  597. });
  598. it('requires the "service" argument to be an Object', function () {
  599. const container = new ServiceContainer();
  600. const throwable = v => () => container.set(String, v);
  601. const error = v =>
  602. format(
  603. 'The second argument of ServicesContainer.set must be ' +
  604. 'an Object, but %s given.',
  605. v,
  606. );
  607. expect(throwable()).to.throw(error('undefined'));
  608. expect(throwable('str')).to.throw(error('"str"'));
  609. expect(throwable(10)).to.throw(error('10'));
  610. expect(throwable(true)).to.throw(error('true'));
  611. expect(throwable(false)).to.throw(error('false'));
  612. expect(throwable(null)).to.throw(error('null'));
  613. expect(throwable([])).to.throw(error('Array'));
  614. throwable({})();
  615. });
  616. describe('Service', function () {
  617. it('returns itself', function () {
  618. class MyService extends Service {}
  619. const container = new ServiceContainer();
  620. const res = container.set(MyService, {});
  621. expect(res).to.be.eq(container);
  622. });
  623. it('sets the given service', function () {
  624. class MyService extends Service {}
  625. const container = new ServiceContainer();
  626. const service = {};
  627. container.set(MyService, service);
  628. const res = container.get(MyService);
  629. expect(res).to.be.eq(service);
  630. });
  631. it('overrides by the given service', function () {
  632. class MyService extends Service {}
  633. const container = new ServiceContainer();
  634. const service1 = {foo: 'bar'};
  635. const service2 = {bar: 'baz'};
  636. container.set(MyService, service1);
  637. container.set(MyService, service2);
  638. const res = container.get(MyService);
  639. expect(res).to.be.eq(service2);
  640. });
  641. });
  642. describe('non-Service', function () {
  643. it('returns itself', function () {
  644. class MyService {}
  645. const container = new ServiceContainer();
  646. const res = container.set(MyService, {});
  647. expect(res).to.be.eq(container);
  648. });
  649. it('sets the given service', function () {
  650. class MyService {}
  651. const container = new ServiceContainer();
  652. const service = {};
  653. container.set(MyService, service);
  654. const res = container.get(MyService);
  655. expect(res).to.be.eq(service);
  656. });
  657. it('overrides by the given service', function () {
  658. class MyService {}
  659. const container = new ServiceContainer();
  660. const service1 = {foo: 'bar'};
  661. const service2 = {bar: 'baz'};
  662. container.set(MyService, service1);
  663. container.set(MyService, service2);
  664. const res = container.get(MyService);
  665. expect(res).to.be.eq(service2);
  666. });
  667. });
  668. });
  669. });