@@ -45,6 +45,7 @@ function handleRequest(item: IService, status: number, body: any) {
4545
4646 // Create the response and return it as a promise.
4747 const response = new Response ( body , { status } ) ;
48+
4849 return Promise . resolve ( response as any ) ;
4950 } ;
5051
@@ -56,98 +57,76 @@ test('test get instance', () => {
5657 expect ( codeSnippetContentsService ) . toBeInstanceOf ( CodeSnippetContentsService ) ;
5758} ) ;
5859
59- test ( 'test getData' , async ( ) => {
60+ describe ( 'test get' , ( ) => {
61+ it ( 'test getData' , async ( ) => {
62+ handleRequest (
63+ codeSnippetContentsService . contentsManager ,
64+ 200 ,
65+ DEFAULT_FILE
66+ ) ;
67+ const model = await codeSnippetContentsService . getData ( '/foo' , 'file' ) ;
68+
69+ expect ( model . content ) . toBe ( DEFAULT_FILE . content ) ;
70+ } ) ;
71+
72+ it ( 'test getDataError' , async ( ) => {
73+ handleRequest (
74+ codeSnippetContentsService . contentsManager ,
75+ 201 ,
76+ DEFAULT_FILE
77+ ) ;
78+
79+ const model = await codeSnippetContentsService . getData ( '/foo' , 'file' ) ;
80+
81+ expect ( model . content ) . toBe ( undefined ) ;
82+ } ) ;
83+ } ) ;
84+
85+ test ( 'test save' , async ( ) => {
6086 handleRequest ( codeSnippetContentsService . contentsManager , 200 , DEFAULT_FILE ) ;
61- const options : Contents . IFetchOptions = { type : 'file' } ;
62- const model = await codeSnippetContentsService . contentsManager . get (
63- '/foo' ,
64- options
65- ) ;
66-
67- console . log ( model . content ) ;
68-
69- // const res = {
70- // name: 'sum_array',
71- // description:
72- // 'Scala program of array. Declare, print, and calculate sum of all elements.',
73- // language: 'Scala',
74- // code: [
75- // 'object ExampleArray1 {',
76- // ' ',
77- // ' def main(args: Array[String]) {',
78- // ' ',
79- // ' var numbers = Array(10,20,30,40,50);',
80- // ' var N:Int=0;',
81- // ' ',
82- // ' //print all array elements',
83- // ' println("All array elements: ");',
84- // ' for ( N <- numbers ) {',
85- // ' println(N);',
86- // ' }',
87- // ' //calculating SUM of all elements',
88- // ' var sum: Int=0;',
89- // ' for ( N <- numbers ) {',
90- // ' sum+=N;',
91- // ' } ',
92- // ' println("Sum of all array elements: "+sum);',
93- // '',
94- // ' }',
95- // '}'
96- // ],
97- // id: 11,
98- // tags: ['math']
99- // };
100- const data = codeSnippetContentsService . getData (
101- 'snippets/sum_array.json' ,
102- 'file'
103- ) ;
104-
105- data . then ( val => {
106- console . log ( val . content ) ;
107- // console.log(JSON.stringify(val));
108- // expect(val).toBe(res);
87+
88+ const saved = await codeSnippetContentsService . save ( 'foo/bar' , {
89+ type : 'file' ,
90+ format : 'text'
10991 } ) ;
92+
93+ expect ( saved . content ) . toBe ( 'hello, world!' ) ;
94+ expect ( saved . path ) . toBe ( 'foo/bar' ) ;
11095} ) ;
11196
112- // test('test save', () => {
113- // const newContent = {
114- // name: 'new_array',
115- // description:
116- // 'Scala program of array. Declare, print, and calculate sum of all elements.',
117- // language: 'Scala',
118- // code: [],
119- // id: 11,
120- // tags: ['math']
121- // };
122- // codeSnippetContentsService.save('snippets/sum_array.json', {
123- // type: 'file',
124- // format: 'text',
125- // content: JSON.stringify(newContent)
126- // });
127-
128- // const data = codeSnippetContentsService.getData(
129- // 'snippets/sum_array.json',
130- // 'file'
131- // );
132-
133- // data.then(val => expect(JSON.parse(val.content).code.length).toBe(0));
134- // });
135-
136- // test('test rename', () => {
137- // const oldPath = 'snippets/sum_array.json';
138- // const newPath = 'snippets/new_array.json';
139- // codeSnippetContentsService.rename(oldPath, newPath);
140-
141- // codeSnippetContentsService
142- // .getData(newPath, 'file')
143- // .then(val => expect(val).toBeTruthy());
144- // });
145-
146- // test('test delete', () => {
147- // const path = 'snippets/sum_array.json';
148- // codeSnippetContentsService.delete(path);
149-
150- // codeSnippetContentsService
151- // .getData(path, 'file')
152- // .then(val => expect(val).toBeNull());
153- // });
97+ describe ( 'test rename' , ( ) => {
98+ it ( 'test rename' , async ( ) => {
99+ handleRequest (
100+ codeSnippetContentsService . contentsManager ,
101+ 200 ,
102+ DEFAULT_FILE
103+ ) ;
104+
105+ const oldPath = 'foo/test' ;
106+ const newPath = 'foo/test2' ;
107+ const renamed = await codeSnippetContentsService . rename ( oldPath , newPath ) ;
108+
109+ expect ( renamed . path ) . toBe ( 'foo/test2' ) ;
110+ } ) ;
111+
112+ it ( 'test rename error' , async ( ) => {
113+ handleRequest (
114+ codeSnippetContentsService . contentsManager ,
115+ 201 ,
116+ DEFAULT_FILE
117+ ) ;
118+
119+ const oldPath = 'foo/test' ;
120+ const newPath = 'foo/test2' ;
121+ const renamed = await codeSnippetContentsService . rename ( oldPath , newPath ) ;
122+
123+ expect ( renamed . content ) . toBe ( undefined ) ;
124+ } ) ;
125+ } ) ;
126+
127+ test ( 'test delete' , async ( ) => {
128+ handleRequest ( codeSnippetContentsService . contentsManager , 200 , DEFAULT_FILE ) ;
129+
130+ const path = 'foo/test' ;
131+ await codeSnippetContentsService . delete ( path ) ;
132+ } ) ;
0 commit comments