Skip to content

Commit

Permalink
apply prettier styles to code generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Schmidt committed May 12, 2018
1 parent bd10a75 commit a27e2f2
Show file tree
Hide file tree
Showing 11 changed files with 418 additions and 555 deletions.
61 changes: 29 additions & 32 deletions generation/__tests__/android.js
Original file line number Diff line number Diff line change
@@ -1,80 +1,77 @@
const fs = require("fs");
const remove = require("remove");
const androidGenerator = require("../adapters/android");
const fs = require('fs');
const remove = require('remove');
const androidGenerator = require('../adapters/android');

describe("Android generation", () => {
describe('Android generation', () => {
let ExampleClass;
let exampleContent;
beforeAll(() => {
// Generate the code to test
fs.mkdirSync("./__tests__/generated-android");
fs.mkdirSync('./__tests__/generated-android');

const files = {
"./fixtures/example.java": "./__tests__/generated-android/example.js"
'./fixtures/example.java': './__tests__/generated-android/example.js'
};

console.log("==> generating android files");
console.log('==> generating android files');
androidGenerator(files);

console.log("==> loading android files");
console.log('==> loading android files');
// Load
ExampleClass = require("./generated-android/example.js");
exampleContent = fs.readFileSync(
"./__tests__/generated-android/example.js",
"utf8"
);
ExampleClass = require('./generated-android/example.js');
exampleContent = fs.readFileSync('./__tests__/generated-android/example.js', 'utf8');
});

afterAll(() => {
// Clean up
remove.removeSync("./__tests__/generated-android");
remove.removeSync('./__tests__/generated-android');
});

describe("methods", () => {
it("should expose the functions", () => {
describe('methods', () => {
it('should expose the functions', () => {
expect(ExampleClass.multiClick).toBeInstanceOf(Function);
});

it("should generate type checks", () => {
it('should generate type checks', () => {
expect(() => {
ExampleClass.multiClick("FOO");
ExampleClass.multiClick('FOO');
}).toThrowErrorMatchingSnapshot();
});

it("should return adapter calls", () => {
it('should return adapter calls', () => {
const result = ExampleClass.multiClick(3);
expect(result.method).toBe("multiClick");
expect(result.target.value).toBe("com.wix.detox.espresso.DetoxAction");
expect(result.args[0].type).toBe("Integer");
expect(result.method).toBe('multiClick');
expect(result.target.value).toBe('com.wix.detox.espresso.DetoxAction');
expect(result.args[0].type).toBe('Integer');
expect(result.args[0].value).toBe(3);

expect(result).toMatchSnapshot();
});

it("should add a sanitizer for the function with the correct name", () => {
it('should add a sanitizer for the function with the correct name', () => {
const fn = ExampleClass.scrollInDirection;

expect(() => {
fn(3, 42);
}).toThrowError();

expect(() => {
fn("down", 42);
fn('down', 42);
}).not.toThrowError();

expect(fn("down", 42)).toMatchSnapshot();
expect(fn('down', 42)).toMatchSnapshot();
});
});

describe("validation", () => {
describe("Matcher<View>", () => {
it("should fail getting no object", () => {
describe('validation', () => {
describe('Matcher<View>', () => {
it('should fail getting no object', () => {
expect(() => {
ExampleClass.matcherForAnd("I am a string", "I am one too");
ExampleClass.matcherForAnd('I am a string', 'I am one too');
}).toThrowErrorMatchingSnapshot();
});

it("should fail with a wrong class", () => {
it('should fail with a wrong class', () => {
class AnotherClass {}
const x = new AnotherClass();

Expand All @@ -88,8 +85,8 @@ describe("Android generation", () => {
class Matcher {
_call() {
return {
target: { type: "Class", value: "Detox.Matcher" },
method: "matchNicely"
target: { type: 'Class', value: 'Detox.Matcher' },
method: 'matchNicely'
};
}
}
Expand Down
159 changes: 73 additions & 86 deletions generation/__tests__/global-functions.js
Original file line number Diff line number Diff line change
@@ -1,149 +1,136 @@
const globals = require("../core/global-functions");
const globals = require('../core/global-functions');

describe("globals", () => {
describe("sanitize_android_direction", () => {
it("should return numbers for strings", () => {
expect(globals.sanitize_android_direction("left")).toBe(1);
expect(globals.sanitize_android_direction("right")).toBe(2);
expect(globals.sanitize_android_direction("up")).toBe(3);
expect(globals.sanitize_android_direction("down")).toBe(4);
describe('globals', () => {
describe('sanitize_android_direction', () => {
it('should return numbers for strings', () => {
expect(globals.sanitize_android_direction('left')).toBe(1);
expect(globals.sanitize_android_direction('right')).toBe(2);
expect(globals.sanitize_android_direction('up')).toBe(3);
expect(globals.sanitize_android_direction('down')).toBe(4);
});

it("should fail with unknown value", () => {
it('should fail with unknown value', () => {
expect(() => {
globals.sanitize_android_direction("kittens");
globals.sanitize_android_direction('kittens');
}).toThrowErrorMatchingSnapshot();
});
});

describe("sanitize_android_edge", () => {
it("should return numbers for strings", () => {
expect(globals.sanitize_android_edge("left")).toBe(1);
expect(globals.sanitize_android_edge("right")).toBe(2);
expect(globals.sanitize_android_edge("top")).toBe(3);
expect(globals.sanitize_android_edge("bottom")).toBe(4);
describe('sanitize_android_edge', () => {
it('should return numbers for strings', () => {
expect(globals.sanitize_android_edge('left')).toBe(1);
expect(globals.sanitize_android_edge('right')).toBe(2);
expect(globals.sanitize_android_edge('top')).toBe(3);
expect(globals.sanitize_android_edge('bottom')).toBe(4);
});

it("should fail with unknown value", () => {
it('should fail with unknown value', () => {
expect(() => {
globals.sanitize_android_edge("kittens");
globals.sanitize_android_edge('kittens');
}).toThrowErrorMatchingSnapshot();
});
});

describe("sanitize_greyDirection", () => {
it("should return numbers for strings", () => {
expect(globals.sanitize_greyDirection("left")).toBe(1);
expect(globals.sanitize_greyDirection("right")).toBe(2);
expect(globals.sanitize_greyDirection("up")).toBe(3);
expect(globals.sanitize_greyDirection("down")).toBe(4);
describe('sanitize_greyDirection', () => {
it('should return numbers for strings', () => {
expect(globals.sanitize_greyDirection('left')).toBe(1);
expect(globals.sanitize_greyDirection('right')).toBe(2);
expect(globals.sanitize_greyDirection('up')).toBe(3);
expect(globals.sanitize_greyDirection('down')).toBe(4);
});

it("should fail with unknown value", () => {
it('should fail with unknown value', () => {
expect(() => {
globals.sanitize_greyDirection("kittens");
globals.sanitize_greyDirection('kittens');
}).toThrowErrorMatchingSnapshot();
});
});

describe("sanitize_greyContentEdge", () => {
it("should return numbers for strings", () => {
expect(globals.sanitize_greyContentEdge("left")).toBe(0);
expect(globals.sanitize_greyContentEdge("right")).toBe(1);
expect(globals.sanitize_greyContentEdge("top")).toBe(2);
expect(globals.sanitize_greyContentEdge("bottom")).toBe(3);
describe('sanitize_greyContentEdge', () => {
it('should return numbers for strings', () => {
expect(globals.sanitize_greyContentEdge('left')).toBe(0);
expect(globals.sanitize_greyContentEdge('right')).toBe(1);
expect(globals.sanitize_greyContentEdge('top')).toBe(2);
expect(globals.sanitize_greyContentEdge('bottom')).toBe(3);
});

it("should fail with unknown value", () => {
it('should fail with unknown value', () => {
expect(() => {
globals.sanitize_greyContentEdge("kittens");
globals.sanitize_greyContentEdge('kittens');
}).toThrowErrorMatchingSnapshot();
});
});

describe("sanitize_uiAccessibilityTraits", () => {
it("should return numbers for traits", () => {
expect(globals.sanitize_uiAccessibilityTraits(["button"])).toBe(1);
describe('sanitize_uiAccessibilityTraits', () => {
it('should return numbers for traits', () => {
expect(globals.sanitize_uiAccessibilityTraits(['button'])).toBe(1);

[
"button",
"link",
"header",
"search",
"image",
"selected",
"plays",
"key",
"text",
"summary",
"disabled",
"frequentUpdates",
"startsMedia",
"adjustable",
"allowsDirectInteraction",
"pageTurn"
].forEach(trait => {
expect(typeof globals.sanitize_uiAccessibilityTraits([trait])).toBe(
"number"
);
'button',
'link',
'header',
'search',
'image',
'selected',
'plays',
'key',
'text',
'summary',
'disabled',
'frequentUpdates',
'startsMedia',
'adjustable',
'allowsDirectInteraction',
'pageTurn'
].forEach((trait) => {
expect(typeof globals.sanitize_uiAccessibilityTraits([trait])).toBe('number');
});
});
it("should combine the traits", () => {
expect(
globals.sanitize_uiAccessibilityTraits([
"summary",
"allowsDirectInteraction"
])
).toBe(16896);
it('should combine the traits', () => {
expect(globals.sanitize_uiAccessibilityTraits(['summary', 'allowsDirectInteraction'])).toBe(16896);
});

it("should throw if unknown trait is accessed", () => {
expect(() =>
globals.sanitize_uiAccessibilityTraits(["unknown"])
).toThrowErrorMatchingSnapshot();
it('should throw if unknown trait is accessed', () => {
expect(() => globals.sanitize_uiAccessibilityTraits(['unknown'])).toThrowErrorMatchingSnapshot();
});
});

const matcherInvocation = {
target: { type: "Class", value: "Detox.Matcher" },
method: "matchNicely"
target: { type: 'Class', value: 'Detox.Matcher' },
method: 'matchNicely'
};
describe("sanitize_matcher", () => {
describe('sanitize_matcher', () => {
it("should return the object if it's no function", () => {
expect(globals.sanitize_matcher({ _call: matcherInvocation })).toEqual(
matcherInvocation
);
expect(globals.sanitize_matcher({ _call: matcherInvocation })).toEqual(matcherInvocation);
});

it("should return the ._call property if it's a function", () => {
const matcherLikeObj = { _call: () => matcherInvocation };
expect(globals.sanitize_matcher(matcherLikeObj)).toEqual(
matcherInvocation
);
expect(globals.sanitize_matcher(matcherLikeObj)).toEqual(matcherInvocation);
});

it("should unwrap the object if it's in an invocation", () => {
const invoke = { type: "Invocation", value: matcherInvocation };
const invoke = { type: 'Invocation', value: matcherInvocation };
const invokeCalled = { _call: invoke };
const invokeThunk = { _call: () => invoke };

expect(globals.sanitize_matcher(invokeCalled)).toEqual(matcherInvocation);
expect(globals.sanitize_matcher(invokeThunk)).toEqual(matcherInvocation);
});

it("should not call on string", () => {
it('should not call on string', () => {
const matcherLikeObj = {
_call: "I am a call"
_call: 'I am a call'
};
expect(globals.sanitize_matcher(matcherLikeObj)).toBe("I am a call");
expect(globals.sanitize_matcher(matcherLikeObj)).toBe('I am a call');
});
});

describe("sanitize_greyElementInteraction", () => {
it("should wrap the argument in an invocation", () => {
expect(globals.sanitize_greyElementInteraction("Foo")).toEqual({
type: "Invocation",
value: "Foo"
describe('sanitize_greyElementInteraction', () => {
it('should wrap the argument in an invocation', () => {
expect(globals.sanitize_greyElementInteraction('Foo')).toEqual({
type: 'Invocation',
value: 'Foo'
});
});
});
Expand Down
18 changes: 7 additions & 11 deletions generation/__tests__/helpers.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
const { methodNameToSnakeCase } = require("../helpers");
const { methodNameToSnakeCase } = require('../helpers');

describe("helpers", () => {
describe("methodNameToSnakeCase", () => {
it("should not fail with empty string", () => {
expect(() => methodNameToSnakeCase("")).not.toThrow();
describe('helpers', () => {
describe('methodNameToSnakeCase', () => {
it('should not fail with empty string', () => {
expect(() => methodNameToSnakeCase('')).not.toThrow();
});

it("should return the correct snake case method name", () => {
expect(
methodNameToSnakeCase(
"actionForScrollInDirection:amount:xOriginStartPercentage:yOriginStartPercentage:"
)
).toMatchSnapshot();
it('should return the correct snake case method name', () => {
expect(methodNameToSnakeCase('actionForScrollInDirection:amount:xOriginStartPercentage:yOriginStartPercentage:')).toMatchSnapshot();
});
});
});
Loading

0 comments on commit a27e2f2

Please sign in to comment.