123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- /*
- MIT License http://www.opensource.org/licenses/mit-license.php
- Author Tobias Koppers @sokra
- */
- "use strict";
- const HookCodeFactory = require("../HookCodeFactory");
- const expectNoSyntaxError = (code) => {
- new Function("a, b, c", code);
- };
- describe("HookCodeFactory", () => {
- describe("callTap", () => {
- const factoryConfigurations = {
- "no args, no intercept": {
- args: [],
- taps: [
- {
- type: "sync"
- },
- {
- type: "async"
- },
- {
- type: "promise"
- }
- ],
- interceptors: []
- },
- "with args, no intercept": {
- args: ["a", "b", "c"],
- taps: [
- {
- type: "sync"
- },
- {
- type: "async"
- },
- {
- type: "promise"
- }
- ],
- interceptors: []
- },
- "with args, with intercept": {
- args: ["a", "b", "c"],
- taps: [
- {
- type: "sync"
- },
- {
- type: "async"
- },
- {
- type: "promise"
- }
- ],
- interceptors: [
- {
- call: () => {},
- tap: () => {}
- },
- {
- tap: () => {}
- },
- {
- call: () => {}
- },
- ]
- }
- }
- for(const configurationName in factoryConfigurations) {
- describe(`(${configurationName})`, () => {
- let factory;
- beforeEach(() => {
- factory = new HookCodeFactory();
- factory.init(factoryConfigurations[configurationName]);
- });
- it("sync without onResult", () => {
- const code = factory.callTap(0, {
- onError: err => `onError(${err});\n`,
- onDone: () => "onDone();\n"
- });
- expect(code).toMatchSnapshot();
- expectNoSyntaxError(code);
- });
- it("sync with onResult", () => {
- const code = factory.callTap(0, {
- onError: err => `onError(${err});\n`,
- onResult: result => `onResult(${result});\n`
- });
- expect(code).toMatchSnapshot();
- expectNoSyntaxError(code);
- });
- it("async without onResult", () => {
- const code = factory.callTap(1, {
- onError: err => `onError(${err});\n`,
- onDone: () => "onDone();\n"
- });
- expect(code).toMatchSnapshot();
- expectNoSyntaxError(code);
- });
- it("async with onResult", () => {
- const code = factory.callTap(1, {
- onError: err => `onError(${err});\n`,
- onResult: result => `onResult(${result});\n`
- });
- expect(code).toMatchSnapshot();
- expectNoSyntaxError(code);
- });
- it("promise without onResult", () => {
- const code = factory.callTap(2, {
- onError: err => `onError(${err});\n`,
- onDone: () => "onDone();\n"
- });
- expect(code).toMatchSnapshot();
- expectNoSyntaxError(code);
- });
- it("promise with onResult", () => {
- const code = factory.callTap(2, {
- onError: err => `onError(${err});\n`,
- onResult: result => `onResult(${result});\n`
- });
- expect(code).toMatchSnapshot();
- expectNoSyntaxError(code);
- });
- });
- }
- });
- describe("taps", () => {
- const factoryConfigurations = {
- "none": {
- args: ["a", "b", "c"],
- taps: [],
- interceptors: []
- },
- "single sync": {
- args: ["a", "b", "c"],
- taps: [
- {
- type: "sync"
- }
- ],
- interceptors: []
- },
- "multiple sync": {
- args: ["a", "b", "c"],
- taps: [
- {
- type: "sync"
- },
- {
- type: "sync"
- },
- {
- type: "sync"
- }
- ],
- interceptors: []
- },
- "single async": {
- args: ["a", "b", "c"],
- taps: [
- {
- type: "async"
- }
- ],
- interceptors: []
- },
- "single promise": {
- args: ["a", "b", "c"],
- taps: [
- {
- type: "promise"
- }
- ],
- interceptors: []
- },
- "mixed": {
- args: ["a", "b", "c"],
- taps: [
- {
- type: "sync"
- },
- {
- type: "async"
- },
- {
- type: "promise"
- }
- ],
- interceptors: []
- },
- "mixed2": {
- args: ["a", "b", "c"],
- taps: [
- {
- type: "async"
- },
- {
- type: "promise"
- },
- {
- type: "sync"
- },
- ],
- interceptors: []
- },
- }
- for(const configurationName in factoryConfigurations) {
- describe(`(${configurationName})`, () => {
- let factory;
- beforeEach(() => {
- factory = new HookCodeFactory();
- factory.init(factoryConfigurations[configurationName]);
- });
- it("callTapsSeries", () => {
- const code = factory.callTapsSeries({
- onError: (i, err) => `onError(${i}, ${err});\n`,
- onResult: (i, result, next, doneBreak) => `onResult(${i}, ${result}, () => {\n${next()}}, () => {\n${doneBreak()}});\n`,
- onDone: () => "onDone();\n",
- rethrowIfPossible: true
- });
- expect(code).toMatchSnapshot();
- expectNoSyntaxError(code);
- });
- it("callTapsParallel", () => {
- const code = factory.callTapsParallel({
- onError: (i, err) => `onError(${i}, ${err});\n`,
- onResult: (i, result, done, doneBreak) => `onResult(${i}, ${result}, () => {\n${done()}}, () => {\n${doneBreak()}});\n`,
- onDone: () => "onDone();\n",
- rethrowIfPossible: true
- });
- expect(code).toMatchSnapshot();
- expectNoSyntaxError(code);
- });
- it("callTapsLooping", () => {
- const code = factory.callTapsLooping({
- onError: (i, err) => `onError(${i}, ${err});\n`,
- onDone: () => "onDone();\n",
- rethrowIfPossible: true
- });
- expect(code).toMatchSnapshot();
- expectNoSyntaxError(code);
- });
- });
- }
- });
- });
|