|
1 | 1 | import { CodeSnippetContentsService } from '../src/CodeSnippetContentsService'; |
| 2 | +import { |
| 3 | + ServerConnection, |
| 4 | + ContentsManager, |
| 5 | + Contents |
| 6 | +} from '@jupyterlab/services'; |
| 7 | +import 'jest'; |
| 8 | +import { Response } from 'node-fetch'; |
| 9 | + |
| 10 | +export const DEFAULT_FILE: Contents.IModel = { |
| 11 | + name: 'test', |
| 12 | + path: 'foo/test', |
| 13 | + type: 'file', |
| 14 | + created: 'yesterday', |
| 15 | + last_modified: 'today', |
| 16 | + writable: true, |
| 17 | + mimetype: 'text/plain', |
| 18 | + content: 'hello, world!', |
| 19 | + format: 'text' |
| 20 | +}; |
| 21 | + |
| 22 | +interface IService { |
| 23 | + readonly serverSettings: ServerConnection.ISettings; |
| 24 | +} |
2 | 25 |
|
3 | 26 | const codeSnippetContentsService = CodeSnippetContentsService.getInstance(); |
| 27 | +const serverSettings = ServerConnection.makeSettings(); |
| 28 | +const manager = new ContentsManager({ serverSettings }); |
| 29 | + |
| 30 | +codeSnippetContentsService.contentsManager = manager; |
| 31 | + |
| 32 | +function handleRequest(item: IService, status: number, body: any) { |
| 33 | + // Store the existing fetch function. |
| 34 | + const oldFetch = item.serverSettings.fetch; |
| 35 | + |
| 36 | + // A single use callback. |
| 37 | + const temp = (info: RequestInfo, init: RequestInit) => { |
| 38 | + // Restore fetch. |
| 39 | + (item.serverSettings as any).fetch = oldFetch; |
4 | 40 |
|
5 | | -test('test getData', () => { |
6 | | - const res = { |
7 | | - name: 'sum_array', |
8 | | - description: |
9 | | - 'Scala program of array. Declare, print, and calculate sum of all elements.', |
10 | | - language: 'Scala', |
11 | | - code: [ |
12 | | - 'object ExampleArray1 {', |
13 | | - ' ', |
14 | | - ' def main(args: Array[String]) {', |
15 | | - ' ', |
16 | | - ' var numbers = Array(10,20,30,40,50);', |
17 | | - ' var N:Int=0;', |
18 | | - ' ', |
19 | | - ' //print all array elements', |
20 | | - ' println("All array elements: ");', |
21 | | - ' for ( N <- numbers ) {', |
22 | | - ' println(N);', |
23 | | - ' }', |
24 | | - ' //calculating SUM of all elements', |
25 | | - ' var sum: Int=0;', |
26 | | - ' for ( N <- numbers ) {', |
27 | | - ' sum+=N;', |
28 | | - ' } ', |
29 | | - ' println("Sum of all array elements: "+sum);', |
30 | | - '', |
31 | | - ' }', |
32 | | - '}' |
33 | | - ], |
34 | | - id: 11, |
35 | | - tags: ['math'] |
| 41 | + // Normalize the body. |
| 42 | + if (typeof body !== 'string') { |
| 43 | + body = JSON.stringify(body); |
| 44 | + } |
| 45 | + |
| 46 | + // Create the response and return it as a promise. |
| 47 | + const response = new Response(body, { status }); |
| 48 | + return Promise.resolve(response as any); |
36 | 49 | }; |
| 50 | + |
| 51 | + // Override the fetch function. |
| 52 | + (item.serverSettings as any).fetch = temp; |
| 53 | +} |
| 54 | + |
| 55 | +test('test get instance', () => { |
| 56 | + expect(codeSnippetContentsService).toBeInstanceOf(CodeSnippetContentsService); |
| 57 | +}); |
| 58 | + |
| 59 | +test('test getData', async () => { |
| 60 | + 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 | + // }; |
37 | 100 | const data = codeSnippetContentsService.getData( |
38 | 101 | 'snippets/sum_array.json', |
39 | 102 | 'file' |
40 | 103 | ); |
41 | 104 |
|
42 | | - data.then(val => expect(val).toBe(res)); |
| 105 | + data.then(val => { |
| 106 | + console.log(val.content); |
| 107 | + // console.log(JSON.stringify(val)); |
| 108 | + // expect(val).toBe(res); |
| 109 | + }); |
43 | 110 | }); |
| 111 | + |
| 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 | +// }); |
0 commit comments