@@ -628,15 +628,6 @@ describe( 'ImageUploadEditing', () => {
628
628
} ) ;
629
629
630
630
it ( 'should upload image with base64 src' , done => {
631
- editor . plugins . get ( 'Clipboard' ) . on ( 'inputTransformation' , ( ) => {
632
- const id = adapterMocks [ 0 ] . loader . id ;
633
- const expected = '<paragraph>bar</paragraph>' +
634
- `[<image src="" uploadId="${ id } " uploadStatus="reading"></image>]` +
635
- '<paragraph>foo</paragraph>' ;
636
-
637
- expectModel ( done , getModelData ( model ) , expected ) ;
638
- } , { priority : 'low' } ) ;
639
-
640
631
setModelData ( model , '<paragraph>[]foo</paragraph>' ) ;
641
632
642
633
const clipboardHtml = `<p>bar</p><img src=${ base64Sample } />` ;
@@ -646,17 +637,16 @@ describe( 'ImageUploadEditing', () => {
646
637
const targetViewRange = editor . editing . mapper . toViewRange ( targetRange ) ;
647
638
648
639
viewDocument . fire ( 'clipboardInput' , { dataTransfer, targetRanges : [ targetViewRange ] } ) ;
649
- } ) ;
650
640
651
- it ( 'should upload image with blob src' , done => {
652
- editor . plugins . get ( 'Clipboard' ) . on ( 'inputTransformation' , ( ) => {
653
- const id = adapterMocks [ 0 ] . loader . id ;
654
- const expected = `[<image src="" uploadId="${ id } " uploadStatus="reading"></image>]` +
655
- '<paragraph>foo</paragraph>' ;
641
+ const id = adapterMocks [ 0 ] . loader . id ;
642
+ const expected = '<paragraph>bar</paragraph>' +
643
+ `[<image src="" uploadId="${ id } " uploadStatus="reading"></image>]` +
644
+ '<paragraph>foo</paragraph>' ;
656
645
657
- expectModel ( done , getModelData ( model ) , expected ) ;
658
- } , { priority : 'low' } ) ;
646
+ expectModel ( done , getModelData ( model ) , expected ) ;
647
+ } ) ;
659
648
649
+ it ( 'should upload image with blob src' , done => {
660
650
setModelData ( model , '<paragraph>[]foo</paragraph>' ) ;
661
651
662
652
const clipboardHtml = `<img src=${ base64ToBlobUrl ( base64Sample ) } />` ;
@@ -666,15 +656,15 @@ describe( 'ImageUploadEditing', () => {
666
656
const targetViewRange = editor . editing . mapper . toViewRange ( targetRange ) ;
667
657
668
658
viewDocument . fire ( 'clipboardInput' , { dataTransfer, targetRanges : [ targetViewRange ] } ) ;
669
- } ) ;
670
659
671
- it ( 'should not upload image if no loader available' , done => {
672
- editor . plugins . get ( 'Clipboard' ) . on ( 'inputTransformation' , ( ) => {
673
- const expected = `[<image src=" ${ base64Sample } "></image>]< paragraph>foo</paragraph>` ;
660
+ const id = adapterMocks [ 0 ] . loader . id ;
661
+ const expected = `[<image src="" uploadId=" ${ id } " uploadStatus="reading"></image>]` +
662
+ '< paragraph>foo</paragraph>' ;
674
663
675
- expectModel ( done , getModelData ( model ) , expected ) ;
676
- } , { priority : 'low' } ) ;
664
+ expectModel ( done , getModelData ( model ) , expected ) ;
665
+ } ) ;
677
666
667
+ it ( 'should not upload image if no loader available' , done => {
678
668
sinon . stub ( fileRepository , 'createLoader' ) . callsFake ( ( ) => null ) ;
679
669
680
670
setModelData ( model , '<paragraph>[]foo</paragraph>' ) ;
@@ -686,6 +676,10 @@ describe( 'ImageUploadEditing', () => {
686
676
const targetViewRange = editor . editing . mapper . toViewRange ( targetRange ) ;
687
677
688
678
viewDocument . fire ( 'clipboardInput' , { dataTransfer, targetRanges : [ targetViewRange ] } ) ;
679
+
680
+ const expected = `[<image src="${ base64Sample } "></image>]<paragraph>foo</paragraph>` ;
681
+
682
+ expectModel ( done , getModelData ( model ) , expected ) ;
689
683
} ) ;
690
684
691
685
it ( 'should not upload and remove image if fetch failed' , done => {
@@ -696,14 +690,6 @@ describe( 'ImageUploadEditing', () => {
696
690
evt . stop ( ) ;
697
691
} , { priority : 'high' } ) ;
698
692
699
- expectData (
700
- '<img src="" uploadId="#loader1_id" uploadProcessed="true"></img>' ,
701
- '[<image src="" uploadId="#loader1_id" uploadStatus="reading"></image>]<paragraph>foo</paragraph>' ,
702
- '<paragraph>[]foo</paragraph>' ,
703
- done ,
704
- false
705
- ) ;
706
-
707
693
setModelData ( model , '<paragraph>[]foo</paragraph>' ) ;
708
694
709
695
const clipboardHtml = `<img src=${ base64Sample } />` ;
@@ -717,7 +703,21 @@ describe( 'ImageUploadEditing', () => {
717
703
return new Promise ( ( res , rej ) => rej ( 'could not fetch' ) ) ;
718
704
} ) ;
719
705
706
+ let content = null ;
707
+ editor . plugins . get ( 'Clipboard' ) . on ( 'inputTransformation' , ( evt , data ) => {
708
+ content = data . content ;
709
+ } ) ;
710
+
720
711
viewDocument . fire ( 'clipboardInput' , { dataTransfer, targetRanges : [ targetViewRange ] } ) ;
712
+
713
+ expectData (
714
+ '<img src="" uploadId="#loader1_id" uploadProcessed="true"></img>' ,
715
+ '[<image src="" uploadId="#loader1_id" uploadStatus="reading"></image>]<paragraph>foo</paragraph>' ,
716
+ '<paragraph>[]foo</paragraph>' ,
717
+ content ,
718
+ done ,
719
+ false
720
+ ) ;
721
721
} ) ;
722
722
723
723
it ( 'should upload only images which were successfully fetched and remove failed ones' , done => {
@@ -738,13 +738,6 @@ describe( 'ImageUploadEditing', () => {
738
738
'[<image src="" uploadId="#loader2_id" uploadStatus="reading"></image>]' +
739
739
'<paragraph>foo</paragraph>' ;
740
740
741
- expectData (
742
- '' ,
743
- expectedModel ,
744
- expectedFinalModel ,
745
- done
746
- ) ;
747
-
748
741
setModelData ( model , '<paragraph>[]foo</paragraph>' ) ;
749
742
750
743
const clipboardHtml = `<p>bar</p><img src=${ base64Sample } />` +
@@ -766,7 +759,20 @@ describe( 'ImageUploadEditing', () => {
766
759
}
767
760
} ) ;
768
761
762
+ let content = null ;
763
+ editor . plugins . get ( 'Clipboard' ) . on ( 'inputTransformation' , ( evt , data ) => {
764
+ content = data . content ;
765
+ } ) ;
766
+
769
767
viewDocument . fire ( 'clipboardInput' , { dataTransfer, targetRanges : [ targetViewRange ] } ) ;
768
+
769
+ expectData (
770
+ '' ,
771
+ expectedModel ,
772
+ expectedFinalModel ,
773
+ content ,
774
+ done
775
+ ) ;
770
776
} ) ;
771
777
772
778
it ( 'should not upload and remove image when `File` constructor is not present' , done => {
@@ -781,26 +787,32 @@ describe( 'ImageUploadEditing', () => {
781
787
evt . stop ( ) ;
782
788
} , { priority : 'high' } ) ;
783
789
790
+ setModelData ( model , '<paragraph>[]foo</paragraph>' ) ;
791
+
792
+ const clipboardHtml = `<img src=${ base64ToBlobUrl ( base64Sample ) } /><p>baz</p>` ;
793
+ const dataTransfer = mockDataTransfer ( clipboardHtml ) ;
794
+
795
+ const targetRange = model . createRange ( model . createPositionAt ( doc . getRoot ( ) , 1 ) , model . createPositionAt ( doc . getRoot ( ) , 1 ) ) ;
796
+ const targetViewRange = editor . editing . mapper . toViewRange ( targetRange ) ;
797
+
798
+ let content = null ;
799
+ editor . plugins . get ( 'Clipboard' ) . on ( 'inputTransformation' , ( evt , data ) => {
800
+ content = data . content ;
801
+ } ) ;
802
+
803
+ viewDocument . fire ( 'clipboardInput' , { dataTransfer, targetRanges : [ targetViewRange ] } ) ;
804
+
784
805
expectData (
785
806
'<img src="" uploadId="#loader1_id" uploadProcessed="true"></img><p>baz</p>' ,
786
807
'<image src="" uploadId="#loader1_id" uploadStatus="reading"></image><paragraph>baz[]foo</paragraph>' ,
787
808
'<paragraph>baz[]foo</paragraph>' ,
809
+ content ,
788
810
err => {
789
811
window . File = fileFn ;
790
812
done ( err ) ;
791
813
} ,
792
814
false
793
815
) ;
794
-
795
- setModelData ( model , '<paragraph>[]foo</paragraph>' ) ;
796
-
797
- const clipboardHtml = `<img src=${ base64ToBlobUrl ( base64Sample ) } /><p>baz</p>` ;
798
- const dataTransfer = mockDataTransfer ( clipboardHtml ) ;
799
-
800
- const targetRange = model . createRange ( model . createPositionAt ( doc . getRoot ( ) , 1 ) , model . createPositionAt ( doc . getRoot ( ) , 1 ) ) ;
801
- const targetViewRange = editor . editing . mapper . toViewRange ( targetRange ) ;
802
-
803
- viewDocument . fire ( 'clipboardInput' , { dataTransfer, targetRanges : [ targetViewRange ] } ) ;
804
816
} ) ;
805
817
806
818
it ( 'should not upload and remove image when `File` constructor is not supported' , done => {
@@ -820,14 +832,6 @@ describe( 'ImageUploadEditing', () => {
820
832
evt . stop ( ) ;
821
833
} , { priority : 'high' } ) ;
822
834
823
- expectData (
824
- '<p>baz</p><img src="" uploadId="#loader1_id" uploadProcessed="true"></img>' ,
825
- '<paragraph>baz</paragraph>[<image src="" uploadId="#loader1_id" uploadStatus="reading"></image>]<paragraph>foo</paragraph>' ,
826
- '<paragraph>baz[]</paragraph><paragraph>foo</paragraph>' ,
827
- done ,
828
- false
829
- ) ;
830
-
831
835
setModelData ( model , '<paragraph>[]foo</paragraph>' ) ;
832
836
833
837
const clipboardHtml = `<p>baz</p><img src=${ base64ToBlobUrl ( base64Sample ) } />` ;
@@ -836,17 +840,25 @@ describe( 'ImageUploadEditing', () => {
836
840
const targetRange = model . createRange ( model . createPositionAt ( doc . getRoot ( ) , 1 ) , model . createPositionAt ( doc . getRoot ( ) , 1 ) ) ;
837
841
const targetViewRange = editor . editing . mapper . toViewRange ( targetRange ) ;
838
842
843
+ let content = null ;
844
+ editor . plugins . get ( 'Clipboard' ) . on ( 'inputTransformation' , ( evt , data ) => {
845
+ content = data . content ;
846
+ } ) ;
847
+
839
848
viewDocument . fire ( 'clipboardInput' , { dataTransfer, targetRanges : [ targetViewRange ] } ) ;
849
+
850
+ expectData (
851
+ '<p>baz</p><img src="" uploadId="#loader1_id" uploadProcessed="true"></img>' ,
852
+ '<paragraph>baz</paragraph>[<image src="" uploadId="#loader1_id" uploadStatus="reading"></image>]<paragraph>foo</paragraph>' ,
853
+ '<paragraph>baz[]</paragraph><paragraph>foo</paragraph>' ,
854
+ content ,
855
+ done ,
856
+ false
857
+ ) ;
840
858
} ) ;
841
859
842
860
// Skip this test on Edge as we mock `File` object there so there is no sense in testing it.
843
861
( isEdgeEnv ? it . skip : it ) ( 'should get file extension from base64 string' , done => {
844
- editor . plugins . get ( 'Clipboard' ) . on ( 'inputTransformation' , ( ) => {
845
- tryExpect ( done , ( ) => {
846
- loader . file . then ( file => expect ( file . name . split ( '.' ) . pop ( ) ) . to . equal ( 'png' ) ) ;
847
- } ) ;
848
- } , { priority : 'low' } ) ;
849
-
850
862
setModelData ( model , '<paragraph>[]foo</paragraph>' ) ;
851
863
852
864
const clipboardHtml = `<img src=${ base64Sample } />` ;
@@ -865,16 +877,14 @@ describe( 'ImageUploadEditing', () => {
865
877
} ) ;
866
878
867
879
viewDocument . fire ( 'clipboardInput' , { dataTransfer, targetRanges : [ targetViewRange ] } ) ;
880
+
881
+ tryExpect ( done , ( ) => {
882
+ loader . file . then ( file => expect ( file . name . split ( '.' ) . pop ( ) ) . to . equal ( 'png' ) ) ;
883
+ } ) ;
868
884
} ) ;
869
885
870
886
// Skip this test on Edge as we mock `File` object there so there is no sense in testing it.
871
887
( isEdgeEnv ? it . skip : it ) ( 'should use fallback file extension' , done => {
872
- editor . plugins . get ( 'Clipboard' ) . on ( 'inputTransformation' , ( ) => {
873
- tryExpect ( done , ( ) => {
874
- loader . file . then ( file => expect ( file . name . split ( '.' ) . pop ( ) ) . to . equal ( 'jpeg' ) ) ;
875
- } ) ;
876
- } , { priority : 'low' } ) ;
877
-
878
888
setModelData ( model , '<paragraph>[]foo</paragraph>' ) ;
879
889
880
890
const clipboardHtml = `<img src=${ base64ToBlobUrl ( base64Sample ) } />` ;
@@ -893,6 +903,10 @@ describe( 'ImageUploadEditing', () => {
893
903
} ) ;
894
904
895
905
viewDocument . fire ( 'clipboardInput' , { dataTransfer, targetRanges : [ targetViewRange ] } ) ;
906
+
907
+ tryExpect ( done , ( ) => {
908
+ loader . file . then ( file => expect ( file . name . split ( '.' ) . pop ( ) ) . to . equal ( 'jpeg' ) ) ;
909
+ } ) ;
896
910
} ) ;
897
911
898
912
it ( 'should not show notification when file loader failed with no error' , done => {
@@ -905,19 +919,6 @@ describe( 'ImageUploadEditing', () => {
905
919
evt . stop ( ) ;
906
920
} , { priority : 'high' } ) ;
907
921
908
- // Check data after paste.
909
- editor . plugins . get ( 'Clipboard' ) . on ( 'inputTransformation' , ( ) => {
910
- adapterMocks [ 0 ] . loader . file . then ( ( ) => {
911
- expect . fail ( 'Promise should be rejected.' ) ;
912
- } ) . catch ( ( ) => {
913
- // Deffer so the promise could be resolved.
914
- setTimeout ( ( ) => {
915
- expect ( notificationsCount ) . to . equal ( 0 ) ;
916
- done ( ) ;
917
- } ) ;
918
- } ) ;
919
- } , { priority : 'low' } ) ;
920
-
921
922
setModelData ( model , '<paragraph>[]foo</paragraph>' ) ;
922
923
923
924
const clipboardHtml = `<img src=${ base64Sample } />` ;
@@ -930,6 +931,16 @@ describe( 'ImageUploadEditing', () => {
930
931
sinon . stub ( window , 'fetch' ) . callsFake ( ( ) => Promise . reject ( ) ) ;
931
932
932
933
viewDocument . fire ( 'clipboardInput' , { dataTransfer, targetRanges : [ targetViewRange ] } ) ;
934
+
935
+ adapterMocks [ 0 ] . loader . file . then ( ( ) => {
936
+ expect . fail ( 'Promise should be rejected.' ) ;
937
+ } ) . catch ( ( ) => {
938
+ // Deffer so the promise could be resolved.
939
+ setTimeout ( ( ) => {
940
+ expect ( notificationsCount ) . to . equal ( 0 ) ;
941
+ done ( ) ;
942
+ } ) ;
943
+ } ) ;
933
944
} ) ;
934
945
935
946
// Helper for validating clipboard and model data as a result of a paste operation. This function checks both clipboard
@@ -939,39 +950,37 @@ describe( 'ImageUploadEditing', () => {
939
950
// @param {String } expectedClipboardData Expected clipboard data on `inputTransformation` event.
940
951
// @param {String } expectedModel Expected model data on `inputTransformation` event.
941
952
// @param {String } expectedModelOnFile Expected model data after all `file.loader` promises are fetched.
953
+ // @param {DocumentFragment } content Content processed in inputTransformation
942
954
// @param {Function } doneFn Callback function to be called when all assertions are done or error occures.
943
955
// @param {Boolean } [onSuccess=true] If `expectedModelOnFile` data should be validated
944
956
// on `loader.file` a promise successful resolution or promise rejection.
945
- function expectData ( expectedClipboardData , expectedModel , expectedModelOnFile , doneFn , onSuccess ) {
946
- // Check data after paste.
947
- editor . plugins . get ( 'Clipboard' ) . on ( 'inputTransformation' , ( evt , data ) => {
948
- const clipboardData = injectLoaderId ( expectedClipboardData || '' , adapterMocks ) ;
949
- const modelData = injectLoaderId ( expectedModel , adapterMocks ) ;
950
- const finalModelData = injectLoaderId ( expectedModelOnFile , adapterMocks ) ;
957
+ function expectData ( expectedClipboardData , expectedModel , expectedModelOnFile , content , doneFn , onSuccess ) {
958
+ const clipboardData = injectLoaderId ( expectedClipboardData || '' , adapterMocks ) ;
959
+ const modelData = injectLoaderId ( expectedModel , adapterMocks ) ;
960
+ const finalModelData = injectLoaderId ( expectedModelOnFile , adapterMocks ) ;
951
961
952
- if ( clipboardData . length ) {
953
- expect ( stringifyView ( data . content ) ) . to . equal ( clipboardData ) ;
954
- }
955
- expect ( getModelData ( model ) ) . to . equal ( modelData ) ;
956
-
957
- if ( onSuccess !== false ) {
958
- adapterMocks [ 0 ] . loader . file . then ( ( ) => {
959
- // Deffer so the promise could be resolved.
960
- setTimeout ( ( ) => {
961
- expectModel ( doneFn , getModelData ( model ) , finalModelData ) ;
962
- } ) ;
962
+ if ( clipboardData . length ) {
963
+ expect ( stringifyView ( content ) ) . to . equal ( clipboardData ) ;
964
+ }
965
+ expect ( getModelData ( model ) ) . to . equal ( modelData ) ;
966
+
967
+ if ( onSuccess !== false ) {
968
+ adapterMocks [ 0 ] . loader . file . then ( ( ) => {
969
+ // Deffer so the promise could be resolved.
970
+ setTimeout ( ( ) => {
971
+ expectModel ( doneFn , getModelData ( model ) , finalModelData ) ;
963
972
} ) ;
964
- } else {
965
- adapterMocks [ 0 ] . loader . file . then ( ( ) => {
966
- expect . fail ( 'The ` loader.file` should be rejected.' ) ;
967
- } ) . catch ( ( ) => {
968
- // Deffer so the promise could be resolved.
969
- setTimeout ( ( ) => {
970
- expectModel ( doneFn , getModelData ( model ) , finalModelData ) ;
971
- } ) ;
973
+ } ) ;
974
+ } else {
975
+ adapterMocks [ 0 ] . loader . file . then ( ( ) => {
976
+ expect . fail ( 'The `loader.file` should be rejected.' ) ;
977
+ } ) . catch ( ( ) => {
978
+ // Deffer so the promise could be resolved.
979
+ setTimeout ( ( ) => {
980
+ expectModel ( doneFn , getModelData ( model ) , finalModelData ) ;
972
981
} ) ;
973
- }
974
- } , { priority : 'low' } ) ;
982
+ } ) ;
983
+ }
975
984
}
976
985
} ) ;
977
986
0 commit comments