|
1 |
| -import { derived, DerivedStore, Store, SubscribableStore, writable, get, readable } from './index'; |
| 1 | +import { |
| 2 | + derived, |
| 3 | + DerivedStore, |
| 4 | + Store, |
| 5 | + SubscribableStore, |
| 6 | + writable, |
| 7 | + get, |
| 8 | + readable, |
| 9 | + batch, |
| 10 | +} from './index'; |
2 | 11 | import { from } from 'rxjs';
|
3 | 12 | import { Component, Injectable } from '@angular/core';
|
4 | 13 | import { TestBed } from '@angular/core/testing';
|
@@ -221,6 +230,20 @@ describe('stores', () => {
|
221 | 230 | expect(values).toEqual([0, 1]); // unsubscribe worked, value 2 was not received
|
222 | 231 | });
|
223 | 232 |
|
| 233 | + it('should work when changing a store in the listener of the store', () => { |
| 234 | + const store = writable(0); |
| 235 | + const calls: number[] = []; |
| 236 | + const unsubscribe = store.subscribe((n) => { |
| 237 | + calls.push(n); |
| 238 | + if (n < 10) { |
| 239 | + store.set(n + 1); |
| 240 | + } |
| 241 | + }); |
| 242 | + expect(calls).toEqual([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); |
| 243 | + expect(get(store)).toEqual(10); |
| 244 | + unsubscribe(); |
| 245 | + }); |
| 246 | + |
224 | 247 | it('should be compatible with rxjs "from" (writable)', () => {
|
225 | 248 | const store = writable(0);
|
226 | 249 | const observable = from(store);
|
@@ -718,4 +741,114 @@ describe('stores', () => {
|
718 | 741 | expect(cleanUpFn).toHaveBeenCalledWith(3);
|
719 | 742 | });
|
720 | 743 | });
|
| 744 | + |
| 745 | + describe('batch', () => { |
| 746 | + it('should work with two writables and a derived', () => { |
| 747 | + const firstName = writable('Arsène'); |
| 748 | + const lastName = writable('Lupin'); |
| 749 | + const fullName = derived([firstName, lastName], ([a, b]) => `${a} ${b}`); |
| 750 | + const values: string[] = []; |
| 751 | + const unsubscribe = fullName.subscribe((value) => values.push(value)); |
| 752 | + const expectedRes = {}; |
| 753 | + const res = batch(() => { |
| 754 | + firstName.set('Sherlock'); |
| 755 | + lastName.set('Holmes'); |
| 756 | + return expectedRes; |
| 757 | + }); |
| 758 | + expect(values).toEqual(['Arsène Lupin', 'Sherlock Holmes']); |
| 759 | + expect(res).toBe(expectedRes); |
| 760 | + unsubscribe(); |
| 761 | + }); |
| 762 | + |
| 763 | + it('should not call listeners multiple times', () => { |
| 764 | + const store = writable(0); |
| 765 | + const calls: number[] = []; |
| 766 | + const unsubscribe = store.subscribe((n) => calls.push(n)); |
| 767 | + expect(calls).toEqual([0]); |
| 768 | + batch(() => { |
| 769 | + store.set(1); |
| 770 | + store.set(2); |
| 771 | + expect(calls).toEqual([0]); |
| 772 | + }); |
| 773 | + expect(calls).toEqual([0, 2]); |
| 774 | + unsubscribe(); |
| 775 | + }); |
| 776 | + |
| 777 | + it('should allow nested calls', () => { |
| 778 | + const store = writable(0); |
| 779 | + const calls: number[] = []; |
| 780 | + const unsubscribe = store.subscribe((n) => calls.push(n)); |
| 781 | + expect(calls).toEqual([0]); |
| 782 | + batch(() => { |
| 783 | + store.set(1); |
| 784 | + store.set(2); |
| 785 | + batch(() => { |
| 786 | + store.set(3); |
| 787 | + store.set(4); |
| 788 | + }); |
| 789 | + store.set(5); |
| 790 | + batch(() => { |
| 791 | + store.set(6); |
| 792 | + store.set(7); |
| 793 | + }); |
| 794 | + expect(calls).toEqual([0]); |
| 795 | + }); |
| 796 | + expect(calls).toEqual([0, 7]); |
| 797 | + unsubscribe(); |
| 798 | + }); |
| 799 | + |
| 800 | + it('should still call multiple times the listeners registered multiple times', () => { |
| 801 | + const store = writable(0); |
| 802 | + const calls: number[] = []; |
| 803 | + const fn = (n: number) => calls.push(n); |
| 804 | + const unsubscribe1 = store.subscribe(fn); |
| 805 | + const unsubscribe2 = store.subscribe(fn); |
| 806 | + expect(calls).toEqual([0, 0]); |
| 807 | + batch(() => { |
| 808 | + store.set(1); |
| 809 | + store.set(2); |
| 810 | + expect(calls).toEqual([0, 0]); |
| 811 | + }); |
| 812 | + expect(calls).toEqual([0, 0, 2, 2]); |
| 813 | + unsubscribe1(); |
| 814 | + unsubscribe2(); |
| 815 | + }); |
| 816 | + |
| 817 | + it('should not call invalidate multiple times', () => { |
| 818 | + const store = writable(0); |
| 819 | + let invalidateCalls = 0; |
| 820 | + const unsubscribe = store.subscribe({ |
| 821 | + next: () => {}, |
| 822 | + invalidate: () => { |
| 823 | + invalidateCalls++; |
| 824 | + }, |
| 825 | + }); |
| 826 | + expect(invalidateCalls).toEqual(0); |
| 827 | + batch(() => { |
| 828 | + expect(invalidateCalls).toEqual(0); |
| 829 | + store.set(1); |
| 830 | + expect(invalidateCalls).toEqual(1); |
| 831 | + store.set(2); |
| 832 | + expect(invalidateCalls).toEqual(1); |
| 833 | + }); |
| 834 | + expect(invalidateCalls).toEqual(1); |
| 835 | + unsubscribe(); |
| 836 | + }); |
| 837 | + |
| 838 | + it('should not call an unregistered listener', () => { |
| 839 | + const store = writable(0); |
| 840 | + const calls: number[] = []; |
| 841 | + const fn = (n: number) => calls.push(n); |
| 842 | + const unsubscribe = store.subscribe(fn); |
| 843 | + expect(calls).toEqual([0]); |
| 844 | + batch(() => { |
| 845 | + store.set(1); |
| 846 | + store.set(2); |
| 847 | + expect(calls).toEqual([0]); |
| 848 | + unsubscribe(); |
| 849 | + store.set(3); |
| 850 | + }); |
| 851 | + expect(calls).toEqual([0]); |
| 852 | + }); |
| 853 | + }); |
721 | 854 | });
|
0 commit comments