Skip to content

Commit

Permalink
feat(js-given): Add type definitions for hidden step
Browse files Browse the repository at this point in the history
  • Loading branch information
flegall committed Jun 21, 2017
1 parent edd35ed commit 2445a6d
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 46 deletions.
89 changes: 69 additions & 20 deletions js-given/type-definitions/index.d.ts
@@ -1,4 +1,4 @@
declare module "js-given" {
declare module 'js-given' {
class Stage {
constructor();

Expand All @@ -16,7 +16,9 @@ declare module "js-given" {
then: () => T;
};

type ScenarioFunc = SimpleScenarioFunc | ParametrizedScenarioFuncWithParameters;
type ScenarioFunc =
| SimpleScenarioFunc
| ParametrizedScenarioFuncWithParameters;

type SimpleScenarioFunc = {
(): void;
Expand All @@ -28,45 +30,91 @@ declare module "js-given" {
};

type Class<T> = {
new(): T;
}
new (): T;
};

type ScenariosDescriptions<G, W, T> = {
(scenariosParam: ScenariosParam<G, W, T>): {[key:string]: ScenarioDescription};
(scenariosParam: ScenariosParam<G, W, T>): {
[key: string]: ScenarioDescription;
};
};

type ScenarioDescription = {
scenarioFunction: ScenarioFunc;
};

type ScenarioOptions = {};
function scenario(options: ScenarioOptions, scenarioFunction: ScenarioFunc): ScenarioDescription;

function scenarios<S>(groupName: string, stagesParam: Class<S>, scenarioFunc: ScenariosDescriptions<S, S, S>): void;
function scenarios<G, W, T>(groupName: string, stagesParam: [Class<G>, Class<W>, Class<T>], scenarioFunc: ScenariosDescriptions<G, W, T>): void;

function scenario(
options: ScenarioOptions,
scenarioFunction: ScenarioFunc
): ScenarioDescription;

function scenarios<S>(
groupName: string,
stagesParam: Class<S>,
scenarioFunc: ScenariosDescriptions<S, S, S>
): void;
function scenarios<G, W, T>(
groupName: string,
stagesParam: [Class<G>, Class<W>, Class<T>],
scenarioFunc: ScenariosDescriptions<G, W, T>
): void;

function setupForRspec(describe: any, it: any): void;
function setupForAva(test: any): void;

type StateType = {
(target: object, decoratedPropertyKey: string, descriptor?: TypedPropertyDescriptor<() => any>): any;
(target: object, decoratedPropertyKey: string, descriptor?: TypedPropertyDescriptor<
() => any
>): any;
addProperty: (target: Class<Stage>, propertyName: string) => void;
};
var State: StateType;

type HiddenType = {
(target: object, decoratedPropertyKey: string, descriptor?: TypedPropertyDescriptor<
() => any
>): any;
addHiddenStep: (target: Class<Stage>, methodName: string) => void;
};
var Hidden: HiddenType;

function doAsync(action: () => Promise<any>): void;

function setupForAva(test: any): void;

function parametrized(parameters: Array<Array<any>>, func: () => void): ParametrizedScenarioFuncWithParameters;
function parametrized1<T>(parameters: T[], func: (a: T) => void): ParametrizedScenarioFuncWithParameters;
function parametrized2<A, B>(parameters: Array<[A, B]>, func: (a: A, b: B) => void): ParametrizedScenarioFuncWithParameters;
function parametrized3<A, B, C>(parameters: Array<[A, B, C]>, func: (a: A, b: B, c: C) => void): ParametrizedScenarioFuncWithParameters;
function parametrized4<A, B, C, D>(parameters: Array<[A, B, C, D]>, func: (a: A, b: B, c: C, d: D) => void): ParametrizedScenarioFuncWithParameters;
function parametrized5<A, B, C, D, E>(parameters: Array<[A, B, C, D, E]>, func: (a: A, b: B, c: C, d: D, e: E) => void): ParametrizedScenarioFuncWithParameters;
function parametrized6<A, B, C, D, E, F>(parameters: Array<[A, B, C, D, E, F]>, func: (a: A, b: B, c: C, d: D, e: E, f: F) => void): ParametrizedScenarioFuncWithParameters;
function parametrized7<A, B, C, D, E, F, G>(parameters: Array<[A, B, C, D, E, F, G]>, func: (a: A, b: B, c: C, d: D, e: E, f: F, g: G) => void): ParametrizedScenarioFuncWithParameters;
function parametrized(
parameters: Array<Array<any>>,
func: () => void
): ParametrizedScenarioFuncWithParameters;
function parametrized1<T>(
parameters: T[],
func: (a: T) => void
): ParametrizedScenarioFuncWithParameters;
function parametrized2<A, B>(
parameters: Array<[A, B]>,
func: (a: A, b: B) => void
): ParametrizedScenarioFuncWithParameters;
function parametrized3<A, B, C>(
parameters: Array<[A, B, C]>,
func: (a: A, b: B, c: C) => void
): ParametrizedScenarioFuncWithParameters;
function parametrized4<A, B, C, D>(
parameters: Array<[A, B, C, D]>,
func: (a: A, b: B, c: C, d: D) => void
): ParametrizedScenarioFuncWithParameters;
function parametrized5<A, B, C, D, E>(
parameters: Array<[A, B, C, D, E]>,
func: (a: A, b: B, c: C, d: D, e: E) => void
): ParametrizedScenarioFuncWithParameters;
function parametrized6<A, B, C, D, E, F>(
parameters: Array<[A, B, C, D, E, F]>,
func: (a: A, b: B, c: C, d: D, e: E, f: F) => void
): ParametrizedScenarioFuncWithParameters;
function parametrized7<A, B, C, D, E, F, G>(
parameters: Array<[A, B, C, D, E, F, G]>,
func: (a: A, b: B, c: C, d: D, e: E, f: F, g: G) => void
): ParametrizedScenarioFuncWithParameters;

export {
Stage,
Expand All @@ -84,5 +132,6 @@ declare module "js-given" {
parametrized5,
parametrized6,
parametrized7,
}
Hidden,
};
}
92 changes: 66 additions & 26 deletions js-given/type-definitions/index.js.flow
Expand Up @@ -4,63 +4,102 @@
* @flow
*/
declare class Stage {
and(): this;
but(): this;
with(): this;
given(): this;
when(): this;
then(): this;
and(): this,
but(): this,
with(): this,
given(): this,
when(): this,
then(): this,
}

type ScenariosParam<G, W, T> = {
given: () => G;
when: () => W;
then: () => T;
given: () => G,
when: () => W,
then: () => T,
};

type ScenarioFunc = SimpleScenarioFunc | ParametrizedScenarioFuncWithParameters;

type SimpleScenarioFunc = {
(): void;
(): void,
};

type ParametrizedScenarioFuncWithParameters = {
func: (...args: any[]) => void;
parameters: Array<Array<any>>;
func: (...args: any[]) => void,
parameters: Array<Array<any>>,
};

type StagesParam<G, W, T> = [Class<G>, Class<W>, Class<T>] | Class<G & W & T>;

type ScenariosDescriptions<G, W, T> = {
(scenariosParam: ScenariosParam<G, W, T>): {[key:string]: ScenarioDescription};
(
scenariosParam: ScenariosParam<G, W, T>
): {[key: string]: ScenarioDescription},
};

type ScenarioDescription = {
scenarioFunction: ScenarioFunc;
scenarioFunction: ScenarioFunc,
};

type ScenarioOptions = {};
declare function scenario(options: $Shape<ScenarioOptions>, scenarioFunction: ScenarioFunc): ScenarioDescription;
declare function scenario(
options: $Shape<ScenarioOptions>,
scenarioFunction: ScenarioFunc
): ScenarioDescription;

declare function scenarios<G: Stage, W: Stage, T: Stage>(groupName: string, stagesParam: StagesParam<G, W, T>, scenarioFunc: ScenariosDescriptions<G, W, T>): void;
declare function scenarios<G: Stage, W: Stage, T: Stage>(
groupName: string,
stagesParam: StagesParam<G, W, T>,
scenarioFunc: ScenariosDescriptions<G, W, T>
): void;

type StateType = {
(target: any, key: string, descriptor: any): any;
addProperty: (target: Class<Stage>, propertyName: string) => void;
(target: any, key: string, descriptor: any): any,
addProperty: (target: Class<Stage>, propertyName: string) => void,
};
declare var State: StateType;

type HiddenType = {
(target: any, key: string, descriptor: any): any,
addHiddenStep: (stageClass: Class<Stage>, methodName: string) => void,
};
declare var Hidden: HiddenType;

declare function setupForRspec(describe: any, it: any): void;
declare function setupForAva(test: any): void;

declare function parametrized(parameters: Array<Array<any>>, func: () => void): ParametrizedScenarioFuncWithParameters;
declare function parametrized1<T>(parameters: T[], func: (a: T) => void): ParametrizedScenarioFuncWithParameters;
declare function parametrized2<A, B>(parameters: Array<[A, B]>, func: (a: A, b: B) => void): ParametrizedScenarioFuncWithParameters;
declare function parametrized3<A, B, C>(parameters: Array<[A, B, C]>, func: (a: A, b: B, c: C) => void): ParametrizedScenarioFuncWithParameters;
declare function parametrized4<A, B, C, D>(parameters: Array<[A, B, C, D]>, func: (a: A, b: B, c: C, d: D) => void): ParametrizedScenarioFuncWithParameters;
declare function parametrized5<A, B, C, D, E>(parameters: Array<[A, B, C, D, E]>, func: (a: A, b: B, c: C, d: D, e: E) => void): ParametrizedScenarioFuncWithParameters;
declare function parametrized6<A, B, C, D, E, F>(parameters: Array<[A, B, C, D, E, F]>, func: (a: A, b: B, c: C, d: D, e: E, f: F) => void): ParametrizedScenarioFuncWithParameters;
declare function parametrized7<A, B, C, D, E, F, G>(parameters: Array<[A, B, C, D, E, F, G]>, func: (a: A, b: B, c: C, d: D, e: E, f: F, g: G) => void): ParametrizedScenarioFuncWithParameters;
declare function parametrized(
parameters: Array<Array<any>>,
func: () => void
): ParametrizedScenarioFuncWithParameters;
declare function parametrized1<T>(
parameters: T[],
func: (a: T) => void
): ParametrizedScenarioFuncWithParameters;
declare function parametrized2<A, B>(
parameters: Array<[A, B]>,
func: (a: A, b: B) => void
): ParametrizedScenarioFuncWithParameters;
declare function parametrized3<A, B, C>(
parameters: Array<[A, B, C]>,
func: (a: A, b: B, c: C) => void
): ParametrizedScenarioFuncWithParameters;
declare function parametrized4<A, B, C, D>(
parameters: Array<[A, B, C, D]>,
func: (a: A, b: B, c: C, d: D) => void
): ParametrizedScenarioFuncWithParameters;
declare function parametrized5<A, B, C, D, E>(
parameters: Array<[A, B, C, D, E]>,
func: (a: A, b: B, c: C, d: D, e: E) => void
): ParametrizedScenarioFuncWithParameters;
declare function parametrized6<A, B, C, D, E, F>(
parameters: Array<[A, B, C, D, E, F]>,
func: (a: A, b: B, c: C, d: D, e: E, f: F) => void
): ParametrizedScenarioFuncWithParameters;
declare function parametrized7<A, B, C, D, E, F, G>(
parameters: Array<[A, B, C, D, E, F, G]>,
func: (a: A, b: B, c: C, d: D, e: E, f: F, g: G) => void
): ParametrizedScenarioFuncWithParameters;

declare function doAsync(action: () => Promise<*>): void;

Expand All @@ -80,4 +119,5 @@ export {
parametrized6,
parametrized7,
doAsync,
Hidden,
};
28 changes: 28 additions & 0 deletions js-given/type-definitions/tests/cosmetic.js
@@ -0,0 +1,28 @@
// @flow
import {Stage, Hidden, scenarios, scenario} from '../index.js';

class SingleStage extends Stage {
@Hidden
someHiddenStep(): this {
return this;
}

someOtherHiddenStep(): this {
return this;
}
}

// $ExpectError
Hidden.addHiddenStep();

// $ExpectError
Hidden.addHiddenStep(SingleStage);

Hidden.addHiddenStep(SingleStage, 'someOtherHiddenStep');

scenarios('group.name', SingleStage, ({given, when, then}) => ({
a_scenario: scenario({}, () => {
given().someHiddenStep();
when().someOtherHiddenStep();
}),
}));

0 comments on commit 2445a6d

Please sign in to comment.