Skip to content

Commit

Permalink
Update middleware tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jareill committed Jul 25, 2024
1 parent b9cdc5a commit 09f8cd0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 36 deletions.
54 changes: 29 additions & 25 deletions src/createSatchel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,17 @@ import type SatchelOptions from './interfaces/SatchelOptions';
import type SatchelInternal from './interfaces/SatchelInternal';
import type Satchel from './interfaces/Satchel';

export function createSatchelInternal(
options: SatchelOptions = {},
// This is only used for testing purposes
finalDispatch?: DispatchFunction
): SatchelInternal {
export function createDispatchWithMiddleware(
middleware: Middleware[],
finalDispatch: DispatchFunction
) {
return middleware.reduceRight(
(next: DispatchFunction, m: Middleware) => m.bind(null, next),
finalDispatch
);
}

export function createSatchelInternal(options: SatchelOptions = {}): SatchelInternal {
const { middleware = [] } = options;

const satchel: SatchelInternal = {
Expand Down Expand Up @@ -98,27 +104,25 @@ export function createSatchelInternal(
__createActionId: (): string => {
return (satchel.__nextActionId++).toString();
},
__finalDispatch:
finalDispatch ??
((actionMessage) => {
let actionId = getPrivateActionId(actionMessage);
let subscribers = satchel.__subscriptions[actionId];

if (subscribers) {
let promises: Promise<any>[] = [];

for (const subscriber of subscribers) {
let returnValue = subscriber(actionMessage);
if (returnValue) {
promises.push(returnValue);
}
}
__finalDispatch: (actionMessage) => {
let actionId = getPrivateActionId(actionMessage);
let subscribers = satchel.__subscriptions[actionId];

if (subscribers) {
let promises: Promise<any>[] = [];

if (promises.length) {
return promises.length == 1 ? promises[0] : Promise.all(promises);
for (const subscriber of subscribers) {
let returnValue = subscriber(actionMessage);
if (returnValue) {
promises.push(returnValue);
}
}
}),

if (promises.length) {
return promises.length == 1 ? promises[0] : Promise.all(promises);
}
}
},
__wrapMutatorTarget: <TAction extends ActionMessage, TReturn>({
actionCreator,
target,
Expand Down Expand Up @@ -183,8 +187,8 @@ export function createSatchelInternal(
__dispatchWithMiddleware: undefined as DispatchFunction,
};

satchel.__dispatchWithMiddleware = middleware.reduceRight(
(next: DispatchFunction, m: Middleware) => m.bind(null, next),
satchel.__dispatchWithMiddleware = createDispatchWithMiddleware(
middleware,
satchel.__finalDispatch
);

Expand Down
22 changes: 11 additions & 11 deletions test/applyMiddlewareTests.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import 'jasmine';
import { DispatchFunction, Middleware } from '../src';
import { createDispatchWithMiddleware } from '../src/createSatchel';
import { createTestSatchel } from './utils/createTestSatchel';

describe('applyMiddleware', () => {
Expand Down Expand Up @@ -34,30 +36,28 @@ describe('applyMiddleware', () => {
expect(actualNext).toBe(satchel.__finalDispatch);
});

it('middleware and finalDispatch get called in order', () => {
it('createDispatchWithMiddleware creates a function that calls middleware and finalDispatch in order', () => {
// Arrange
let sequence: string[] = [];

const middleware = [
(next: any, actionMessage: any) => {
(next: DispatchFunction, actionMessage: any) => {
sequence.push('middleware1');
next(actionMessage);
},
(next: any, actionMessage: any) => {
(next: DispatchFunction, actionMessage: any) => {
sequence.push('middleware2');
next(actionMessage);
},
];

const satchel = createTestSatchel(
{ middleware },
jasmine.createSpy('finalDispatch').and.callFake(() => {
sequence.push('finalDispatch');
})
);
const finalDispatch = (_actionMessage: any) => {
sequence.push('finalDispatch');
};

const testDispatchWithMiddleware = createDispatchWithMiddleware(middleware, finalDispatch);

// Act
satchel.__dispatchWithMiddleware({});
testDispatchWithMiddleware({});

// Assert
expect(sequence).toEqual(['middleware1', 'middleware2', 'finalDispatch']);
Expand Down

0 comments on commit 09f8cd0

Please sign in to comment.