1+ // LICENSE : MIT
2+ "use strict" ;
3+ import assert from "power-assert" ;
4+ import Junction from "../../src/connect/junction" ;
5+ describe ( "junction" , function ( ) {
6+ context ( "when register middlewares" , function ( ) {
7+ it ( "should connect middleware, the order is register" , function ( done ) {
8+ let junction = new Junction ( ) ;
9+ junction . use ( function errorHandling ( error , text , next ) {
10+ next ( error ) ;
11+ } ) ;
12+ junction . use ( function toUpper ( res , next ) {
13+ res . value = res . value . toLocaleUpperCase ( ) ;
14+ next ( ) ;
15+ } ) ;
16+ junction . use ( function addDesu ( res , next ) {
17+ res . value += " suffix" ;
18+ next ( ) ;
19+ } ) ;
20+ junction . process ( "text" , ( error , result ) => {
21+ if ( error ) {
22+ return done ( error ) ;
23+ }
24+ assert . equal ( result . value , "TEXT suffix" ) ;
25+ done ( ) ;
26+ } ) ;
27+ } ) ;
28+ } ) ;
29+ context ( "when occur error in middleware" , function ( ) {
30+ it ( "should call errorHandling middleware" , function ( done ) {
31+ let junction = new Junction ( ) ;
32+ junction . use ( function toUpper ( res ) {
33+ throw new Error ( "error on " + res ) ;
34+ } ) ;
35+ junction . use ( function errorHandling ( error , res , next ) {
36+ assert ( error instanceof Error ) ;
37+ assert . equal ( res . value , "text" ) ;
38+ next ( ) ;
39+ } ) ;
40+ junction . process ( "text" , ( error , res ) => {
41+ if ( error ) {
42+ return done ( error ) ;
43+ }
44+ assert . equal ( res . value , "text" ) ;
45+ done ( ) ;
46+ } ) ;
47+ } ) ;
48+ } ) ;
49+ } ) ;
0 commit comments