@@ -908,57 +908,106 @@ describe('utilities', function () {
908
908
delete chai . Assertion . prototype . foo ;
909
909
} ) ;
910
910
911
- it ( 'overwriteChainableMethod' , function ( ) {
912
- chai . use ( function ( _chai , _ ) {
913
- _chai . Assertion . addChainableMethod ( 'x' ,
914
- function ( ) {
915
- new chai . Assertion ( this . _obj ) . to . be . equal ( 'x' ) ;
916
- }
917
- , function ( ) {
918
- this . _obj = this . _obj || { } ;
919
- this . _obj . __x = 'X!'
920
- }
921
- ) ;
911
+ describe ( 'overwriteChainableMethod' , function ( ) {
912
+ var assertionConstructor ;
913
+ var utils ;
922
914
923
- _chai . Assertion . overwriteChainableMethod ( 'x' ,
924
- function ( _super ) {
925
- return function ( ) {
926
- if ( _ . flag ( this , 'marked' ) ) {
927
- new chai . Assertion ( this . _obj ) . to . be . equal ( 'spot' ) ;
928
- } else {
915
+ before ( function ( ) {
916
+ chai . use ( function ( _chai , _utils ) {
917
+ assertionConstructor = _chai . Assertion ;
918
+ utils = _utils ;
919
+
920
+ _chai . Assertion . addChainableMethod ( 'x' ,
921
+ function ( ) {
922
+ new chai . Assertion ( this . _obj ) . to . be . equal ( 'x' ) ;
923
+ }
924
+ , function ( ) {
925
+ this . _obj = this . _obj || { } ;
926
+ this . _obj . __x = 'X!'
927
+ }
928
+ ) ;
929
+
930
+ _chai . Assertion . overwriteChainableMethod ( 'x' ,
931
+ function ( _super ) {
932
+ return function ( ) {
933
+ utils . flag ( this , 'mySpecificFlag' , 'value1' ) ;
934
+ utils . flag ( this , 'ultraSpecificFlag' , 'value2' ) ;
935
+
936
+ if ( utils . flag ( this , 'marked' ) ) {
937
+ new chai . Assertion ( this . _obj ) . to . be . equal ( 'spot' ) ;
938
+ } else {
939
+ _super . apply ( this , arguments ) ;
940
+ }
941
+ } ;
942
+ }
943
+ , function ( _super ) {
944
+ return function ( ) {
945
+ utils . flag ( this , 'message' , 'x marks the spot' ) ;
929
946
_super . apply ( this , arguments ) ;
930
- }
931
- } ;
932
- }
933
- , function ( _super ) {
934
- return function ( ) {
935
- _ . flag ( this , 'message' , 'x marks the spot' ) ;
936
- _super . apply ( this , arguments ) ;
937
- } ;
938
- }
939
- ) ;
947
+ } ;
948
+ }
949
+ ) ;
940
950
951
+ _chai . Assertion . addMethod ( 'checkFlags' , function ( ) {
952
+ this . assert (
953
+ utils . flag ( this , 'mySpecificFlag' ) === 'value1' &&
954
+ utils . flag ( this , 'ultraSpecificFlag' ) === 'value2' &&
955
+ utils . flag ( this , 'message' ) === 'x marks the spot'
956
+ , 'expected assertion to have specific flags'
957
+ , "this doesn't matter"
958
+ ) ;
959
+ } ) ;
960
+ } ) ;
961
+ } ) ;
962
+
963
+ after ( function ( ) {
964
+ delete chai . Assertion . prototype . x ;
965
+ delete chai . Assertion . prototype . checkFlags ;
966
+ } ) ;
967
+
968
+ it ( 'overwriteChainableMethod' , function ( ) {
941
969
// Make sure the original behavior of 'x' remains the same
942
970
expect ( 'foo' ) . x . to . equal ( "foo" ) ;
943
971
expect ( "x" ) . x ( ) ;
944
972
expect ( function ( ) {
945
973
expect ( "foo" ) . x ( ) ;
946
- } ) . to . throw ( _chai . AssertionError ) ;
974
+ } ) . to . throw ( chai . AssertionError ) ;
947
975
var obj = { } ;
948
976
expect ( obj ) . x . to . be . ok ;
949
977
expect ( obj ) . to . have . property ( '__x' , 'X!' ) ;
950
978
951
979
// Test the new behavior of 'x'
952
980
var assertion = expect ( 'foo' ) . x . to . be . ok ;
953
- expect ( _ . flag ( assertion , 'message' ) ) . to . equal ( 'x marks the spot' ) ;
981
+ expect ( utils . flag ( assertion , 'message' ) ) . to . equal ( 'x marks the spot' ) ;
954
982
expect ( function ( ) {
955
983
var assertion = expect ( 'x' ) ;
956
- _ . flag ( assertion , 'marked' , true ) ;
984
+ utils . flag ( assertion , 'marked' , true ) ;
957
985
assertion . x ( )
958
- } ) . to . throw ( _chai . AssertionError ) ;
986
+ } ) . to . throw ( chai . AssertionError ) ;
959
987
} ) ;
960
988
961
- delete chai . Assertion . prototype . x ;
989
+ it ( 'should return a new assertion with flags copied over' , function ( ) {
990
+ var assertion1 = expect ( 'x' ) ;
991
+ var assertion2 = assertion1 . x ( ) ;
992
+
993
+ chai . config . proxyExcludedKeys . push ( 'nodeType' ) ;
994
+
995
+ // Checking if a new assertion was returned
996
+ expect ( assertion1 ) . to . not . be . equal ( assertion2 ) ;
997
+
998
+ // Check if flags were copied
999
+ assertion2 . checkFlags ( ) ;
1000
+
1001
+ // Checking if it's really an instance of an Assertion
1002
+ expect ( assertion2 ) . to . be . instanceOf ( assertionConstructor ) ;
1003
+
1004
+ // Test chaining `.length` after a method to guarantee it is not a function's `length`
1005
+ expect ( 'x' ) . to . be . x ( ) . length . above ( 0 ) ;
1006
+
1007
+ // Ensure that foo returns an Assertion (not a function)
1008
+ expect ( expect ( 'x' ) . x ( ) ) . to . be . an . instanceOf ( assertionConstructor ) ;
1009
+ expect ( expect ( 'x' ) . x ) . to . be . an . instanceOf ( assertionConstructor ) ;
1010
+ } ) ;
962
1011
} ) ;
963
1012
964
1013
it ( 'compareByInspect' , function ( ) {
0 commit comments