@@ -56,15 +56,96 @@ describe('test getter and setter', () => {
5656 } ) ;
5757} ) ;
5858
59- // describe('test snippet manipulation', () => {
60- // it('test add snippet', () => {
61- // const newSnippet = {
62- // name: 'test_snippet_three',
63- // description: 'testing code snippet widget model',
64- // language: 'Python',
65- // code: ["print('testing')"],
66- // id: 2
67- // };
68- // codeSnippetWidgetModel.addSnippet(newSnippet);
69- // });
70- // });
59+ describe ( 'test snippet manipulation' , ( ) => {
60+ it ( 'test add insert snippet' , ( ) => {
61+ const insertSnippet = jest . spyOn (
62+ CodeSnippetWidgetModel . prototype as any ,
63+ 'insertSnippet'
64+ ) ;
65+
66+ const newSnippet = {
67+ name : 'test_snippet_three' ,
68+ description : 'testing code snippet widget model' ,
69+ language : 'Python' ,
70+ code : [ "print('testing')" ] ,
71+ id : 2
72+ } ;
73+ codeSnippetWidgetModel . addSnippet ( newSnippet , newSnippet . id ) ;
74+
75+ expect (
76+ codeSnippetWidgetModel . snippets [
77+ codeSnippetWidgetModel . snippets . length - 1
78+ ]
79+ ) . toBe ( newSnippet ) ;
80+
81+ expect ( insertSnippet ) . toHaveBeenCalled ( ) ;
82+ } ) ;
83+
84+ it ( 'test delete snippet' , ( ) => {
85+ const originalSnippet = [
86+ {
87+ name : 'test_snippet_one' ,
88+ description : 'testing code snippet widget model' ,
89+ language : 'Python' ,
90+ code : [ "print('hello world')" ] ,
91+ id : 0 ,
92+ tags : [ 'test' ]
93+ } ,
94+ {
95+ name : 'test_snippet_two' ,
96+ description : 'testing code snippet widget model' ,
97+ language : 'Python' ,
98+ code : [ "print('hello world again!')" ] ,
99+ id : 1
100+ }
101+ ] ;
102+ codeSnippetWidgetModel . deleteSnippet ( - 1 ) ;
103+ expect ( codeSnippetWidgetModel . snippets ) . toStrictEqual ( originalSnippet ) ;
104+ } ) ;
105+
106+ it ( 'test move snippet' , ( ) => {
107+ codeSnippetWidgetModel . moveSnippet ( 0 , 3 ) ;
108+ expect ( codeSnippetWidgetModel . snippets [ 1 ] . id ) . toBe ( 2 ) ;
109+ } ) ;
110+
111+ it ( 'test reorder snippet' , ( ) => {
112+ codeSnippetWidgetModel . reorderSnippet ( ) ;
113+ expect ( codeSnippetWidgetModel . snippets [ 1 ] . id ) . toBe ( 1 ) ;
114+ } ) ;
115+
116+ it ( 'test rename snippet' , ( ) => {
117+ codeSnippetWidgetModel . renameSnippet ( 'test_snippet_one' , 'test_snippet' ) ;
118+
119+ expect ( codeSnippetWidgetModel . snippets [ 1 ] . name ) . toBe ( 'test_snippet' ) ;
120+ } ) ;
121+
122+ it ( 'test sort snippet' , ( ) => {
123+ const unsortedSnippets : ICodeSnippet [ ] = [
124+ {
125+ name : 'test_snippet' ,
126+ description : 'testing code snippet widget model' ,
127+ language : 'Python' ,
128+ code : [ "print('hello world')" ] ,
129+ id : 1 ,
130+ tags : [ 'test' ]
131+ } ,
132+ {
133+ name : 'test_snippet_two' ,
134+ description : 'testing code snippet widget model' ,
135+ language : 'Python' ,
136+ code : [ "print('hello world again!')" ] ,
137+ id : 0
138+ }
139+ ] ;
140+ codeSnippetWidgetModel . snippets = unsortedSnippets ;
141+
142+ codeSnippetWidgetModel . sortSnippets ( ) ;
143+ expect ( codeSnippetWidgetModel . snippets [ 0 ] . id ) . toBe ( 0 ) ;
144+ } ) ;
145+
146+ it ( 'test clear snippet' , ( ) => {
147+ codeSnippetWidgetModel . clearSnippets ( ) ;
148+
149+ expect ( codeSnippetWidgetModel . snippets . length ) . toBe ( 0 ) ;
150+ } ) ;
151+ } ) ;
0 commit comments