Skip to content

Commit 21d072a

Browse files
committed
[tests] and docs for transaction listener mutation
1 parent 1ff948b commit 21d072a

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

src/types/docs/store.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4630,6 +4630,10 @@
46304630
* the transaction. Since this is called at the start, they will both be
46314631
* `false`!
46324632
*
4633+
* Note that a TransactionListener added to the Store with this method can
4634+
* mutate the Store, and its changes will be treated as part of the
4635+
* transaction that is starting.
4636+
*
46334637
* @returns A unique Id for the listener that can later be used to remove it.
46344638
* @example
46354639
* This example registers a listener that is called at start end of the
@@ -4681,6 +4685,11 @@
46814685
* value of `cellsTouched` and `valuesTouched` in the listener will be `false`
46824686
* because all changes have been reverted.
46834687
*
4688+
* Note that a TransactionListener added to the Store with this method can
4689+
* mutate the Store itself, and its changes will be treated as part of the
4690+
* transaction that is starting (and may fire non-mutating listeners after
4691+
* this).
4692+
*
46844693
* @returns A unique Id for the listener that can later be used to remove it.
46854694
* @example
46864695
* This example registers a listener that is called at the end of the
@@ -4773,6 +4782,9 @@
47734782
* value of `cellsTouched` and `valuesTouched` in the listener will be `false`
47744783
* because all changes have been reverted.
47754784
*
4785+
* Note that a TransactionListener added to the Store with this method
4786+
* _cannot_ mutate the Store itself, and attempts to do so will fail silently.
4787+
*
47764788
* @returns A unique Id for the listener that can later be used to remove it.
47774789
* @example
47784790
* This example registers a listener that is called at the end of the

test/unit/store-listeners.test.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3132,7 +3132,7 @@ describe('Listeners', () => {
31323132
});
31333133
});
31343134

3135-
describe('Will and did finish transaction', () => {
3135+
describe('transaction', () => {
31363136
beforeEach(() => {
31373137
store = createStore();
31383138
listener = createStoreListener(store);
@@ -4901,6 +4901,45 @@ describe('Mutating listeners', () => {
49014901
});
49024902
});
49034903

4904+
describe('transaction', () => {
4905+
beforeEach(() => {
4906+
store = createStore();
4907+
});
4908+
4909+
test('start can mutate', () => {
4910+
const listener = jest.fn(() => {
4911+
store.setValue('mutated', true);
4912+
});
4913+
store.addStartTransactionListener(listener);
4914+
store.setCell('t1', 'r1', 'c1', 'r1');
4915+
expect(listener).toHaveBeenCalledTimes(1);
4916+
expect(store.getTables()).toEqual({t1: {r1: {c1: 'r1'}}});
4917+
expect(store.getValues()).toEqual({mutated: true});
4918+
});
4919+
4920+
test('willFinish can mutate', () => {
4921+
const listener = jest.fn(() => {
4922+
store.setValue('mutated', true);
4923+
});
4924+
store.addWillFinishTransactionListener(listener);
4925+
store.setCell('t1', 'r1', 'c1', 'r1');
4926+
expect(listener).toHaveBeenCalledTimes(1);
4927+
expect(store.getTables()).toEqual({t1: {r1: {c1: 'r1'}}});
4928+
expect(store.getValues()).toEqual({mutated: true});
4929+
});
4930+
4931+
test('didFinish cannot mutate', () => {
4932+
const listener = jest.fn(() => {
4933+
store.setValue('mutated', true);
4934+
});
4935+
store.addDidFinishTransactionListener(listener);
4936+
store.setCell('t1', 'r1', 'c1', 'r1');
4937+
expect(listener).toHaveBeenCalledTimes(1);
4938+
expect(store.getTables()).toEqual({t1: {r1: {c1: 'r1'}}});
4939+
expect(store.getValues()).toEqual({});
4940+
});
4941+
});
4942+
49044943
describe('Miscellaneous', () => {
49054944
beforeEach(() => {
49064945
store = createStore();

0 commit comments

Comments
 (0)