microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
test/resources/recordingsHelper.ts
69lines · modeblame
14c2611bdigeff10 years ago | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. | |
| 3 | | |
396b6627Vladimir Kotikov8 years ago | 4 | import * as fs from "fs"; |
| 5 | import * as path from "path"; | |
| 6 | | |
678db279Artem Egorov8 years ago | 7 | const RECORDINGS_ROOT = path.resolve(__dirname, "processExecutionsRecordings"); |
396b6627Vladimir Kotikov8 years ago | 8 | |
14c2611bdigeff10 years ago | 9 | interface TestUsingRecording { |
| 10 | (expectation: string, recordingNames: string[], assertion?: () => void): Mocha.ITest; | |
| 11 | (expectation: string, recordingNames: string[], assertion?: (done: MochaDone) => void): Mocha.ITest; | |
| 12 | only(expectation: string, recordingNames: string[], assertion?: () => void): Mocha.ITest; | |
| 13 | only(expectation: string, recordingNames: string[], assertion?: (done: MochaDone) => void): Mocha.ITest; | |
| 14 | skip(expectation: string, recordingNames: string[], assertion?: () => void): void; | |
| 15 | skip(expectation: string, recordingNames: string[], assertion?: (done: MochaDone) => void): void; | |
| 16 | } | |
| 17 | | |
| 18 | export interface IRecordingConsumer { | |
| 19 | loadRecordingFromName(recordingName: string): Q.Promise<void>; | |
396b6627Vladimir Kotikov8 years ago | 20 | loadRecordingFromString(recordingName: string): Q.Promise<void>; |
14c2611bdigeff10 years ago | 21 | } |
| 22 | | |
| 23 | /* This class makes it easy to create a test using a recording. Recommended usage is: | |
| 24 | const testWithRecordings = new RecordingsHelper(() => recordingConsumer).test; | |
| 25 | testWithRecordings("expects to do some test thing", | |
| 26 | [ | |
| 27 | "path/to/recording", | |
| 28 | "path/to/recording" | |
| 29 | ], () => { | |
| 30 | // test code here | |
| 31 | }); | |
| 32 | */ | |
| 33 | export class RecordingsHelper { | |
| 34 | public test: TestUsingRecording; | |
| 35 | | |
396b6627Vladimir Kotikov8 years ago | 36 | private recordings: { [name: string]: string }; |
| 37 | | |
14c2611bdigeff10 years ago | 38 | constructor(private getRecordingConsumer: () => IRecordingConsumer) { |
396b6627Vladimir Kotikov8 years ago | 39 | this.recordings = {}; |
14c2611bdigeff10 years ago | 40 | this.initializeTest(); |
| 41 | } | |
| 42 | | |
| 43 | private initializeTest(): void { | |
| 44 | this.test = <TestUsingRecording>((testName: string, recordingNames: string[], code: () => Q.Promise<void>): void => { | |
a4a82040digeff10 years ago | 45 | if (code.length !== 0) { // Check how many arguments the function has |
| 46 | throw new RangeError("(done: mochaDone) parameter is not supported. Please return a promise instead."); | |
| 47 | } | |
| 48 | const recordingsHelper = this; | |
14c2611bdigeff10 years ago | 49 | recordingNames.forEach(recordingName => { |
396b6627Vladimir Kotikov8 years ago | 50 | |
| 51 | let recording: string = this.recordings[recordingName]; | |
| 52 | if (!recording) { | |
| 53 | recording = fs.readFileSync(path.resolve(RECORDINGS_ROOT, recordingName) + ".json", "utf8"); | |
| 54 | this.recordings[recordingName] = recording; | |
| 55 | } | |
| 56 | | |
a4a82040digeff10 years ago | 57 | test(`${testName} using recording ${recordingName}`, function () { // We use function () because we need the this pointer |
396b6627Vladimir Kotikov8 years ago | 58 | return recordingsHelper |
| 59 | .getRecordingConsumer() | |
| 60 | .loadRecordingFromString(recording) | |
| 61 | .then(code.bind(this)); | |
14c2611bdigeff10 years ago | 62 | }); |
| 63 | }); | |
| 64 | }); | |
| 65 | this.test.skip = (expectation: string, recordingNames: string[], assertion?: (done: MochaDone) => void) => { | |
| 66 | test.skip(expectation, assertion); | |
| 67 | }; | |
| 68 | } | |
396b6627Vladimir Kotikov8 years ago | 69 | } |