microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.5.7

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

test/resources/recordingsHelper.ts

69lines · modeblame

14c2611bdigeff10 years ago1// 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 ago4import * as fs from "fs";
5import * as path from "path";
6
678db279Artem Egorov8 years ago7const RECORDINGS_ROOT = path.resolve(__dirname, "processExecutionsRecordings");
396b6627Vladimir Kotikov8 years ago8
14c2611bdigeff10 years ago9interface TestUsingRecording {
10(expectation: string, recordingNames: string[], assertion?: () => void): Mocha.ITest;
11(expectation: string, recordingNames: string[], assertion?: (done: MochaDone) => void): Mocha.ITest;
12only(expectation: string, recordingNames: string[], assertion?: () => void): Mocha.ITest;
13only(expectation: string, recordingNames: string[], assertion?: (done: MochaDone) => void): Mocha.ITest;
14skip(expectation: string, recordingNames: string[], assertion?: () => void): void;
15skip(expectation: string, recordingNames: string[], assertion?: (done: MochaDone) => void): void;
16}
17
18export interface IRecordingConsumer {
19loadRecordingFromName(recordingName: string): Q.Promise<void>;
396b6627Vladimir Kotikov8 years ago20loadRecordingFromString(recordingName: string): Q.Promise<void>;
14c2611bdigeff10 years ago21}
22
23/* This class makes it easy to create a test using a recording. Recommended usage is:
24const testWithRecordings = new RecordingsHelper(() => recordingConsumer).test;
25testWithRecordings("expects to do some test thing",
26[
27"path/to/recording",
28"path/to/recording"
29], () => {
30// test code here
31});
32*/
33export class RecordingsHelper {
34public test: TestUsingRecording;
35
396b6627Vladimir Kotikov8 years ago36private recordings: { [name: string]: string };
37
14c2611bdigeff10 years ago38constructor(private getRecordingConsumer: () => IRecordingConsumer) {
396b6627Vladimir Kotikov8 years ago39this.recordings = {};
14c2611bdigeff10 years ago40this.initializeTest();
41}
42
43private initializeTest(): void {
44this.test = <TestUsingRecording>((testName: string, recordingNames: string[], code: () => Q.Promise<void>): void => {
a4a82040digeff10 years ago45if (code.length !== 0) { // Check how many arguments the function has
46throw new RangeError("(done: mochaDone) parameter is not supported. Please return a promise instead.");
47}
48const recordingsHelper = this;
14c2611bdigeff10 years ago49recordingNames.forEach(recordingName => {
396b6627Vladimir Kotikov8 years ago50
51let recording: string = this.recordings[recordingName];
52if (!recording) {
53recording = fs.readFileSync(path.resolve(RECORDINGS_ROOT, recordingName) + ".json", "utf8");
54this.recordings[recordingName] = recording;
55}
56
a4a82040digeff10 years ago57test(`${testName} using recording ${recordingName}`, function () { // We use function () because we need the this pointer
396b6627Vladimir Kotikov8 years ago58return recordingsHelper
59.getRecordingConsumer()
60.loadRecordingFromString(recording)
61.then(code.bind(this));
14c2611bdigeff10 years ago62});
63});
64});
65this.test.skip = (expectation: string, recordingNames: string[], assertion?: (done: MochaDone) => void) => {
66test.skip(expectation, assertion);
67};
68}
396b6627Vladimir Kotikov8 years ago69}