jwillinghalpern / fm-mock

A library for mocking the window.FileMaker object. This lets you develop FileMaker webviewer apps in the browser.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

add mockGoferScript export

jwillinghalpern opened this issue · comments

This would be a convenience function to combine with the fm-gofer library.

The usage should look something like this:

Example interface describing API:

Example usage:

// mock data example
// (use similar call signature to return a string or number,
//    they should all be stringified automatically to simulate
//    how FM passes only strings to JS functions)
mockGoferScript(
    'Fetch Data',
    {
        "name": "Josh",
        "animal": "Humanish",
    },
   { delay: 1500 } // simulate slow FM script
)

// custom callback example 
mockGoferScript(
    'Fetch Data',
    // if possible, these three params should be automatically destructured and passed to this fn (via currying?)
    (callbackName, promiseID, parameter) => {
        // do some custom logic, perhaps simulating intermittent record lock errors
        window[callbackName](promiseID, JSON.stringify({name: 'Josh', animal: 'Humanish'});
    },
    { delay: 123 }
)

It should be able to accept any of the following as the second param

  1. a void fn with args callbackName, promiseID, and parameter
  2. an object or array, which will be stringified and returned via callback automatically
  3. a string, which will be returned verbatim automatically
  4. a number whiwh will be stringified and returned automatically.

resultFromFM should be optional and possibly should live in the options object. One advantage of putting it in the options object is it will have a key name, which will be more readable.

current:

declare const mockGoferScript: (scriptName: string, resultFromFM: Record<string, unknown> | any[] | number | string | ResultFunction | AsyncResultFunction, options?: {
    delay?: number | undefined;
    returnError?: boolean | undefined;
    logParams?: boolean | undefined;
}) => void;

proposed:

declare const mockGoferScript: (scriptName: string, resultFromFM?: Record<string, unknown> | any[] | number | string | ResultFunction | AsyncResultFunction, options?: {
    delay?: number | undefined;
    returnError?: boolean | undefined;
    logParams?: boolean | undefined;
}) => void;

// or...
declare const mockGoferScript: (scriptName: string, options?: {
    resultFromFM?: Record<string, unknown> | any[] | number | string | ResultFunction | AsyncResultFunction;
    delay?: number | undefined;
    returnError?: boolean | undefined;
    logParams?: boolean | undefined;
}) => void;

consider adding options param to regular mockScript, but exclude returnError, because that's gofer specific.

Maybe add ability to import json data from a file instead of having to inline it in the mocks (even though you could mock one script per file, theoretically).

I changed my mind about adding support for a path to a mock json file because this can already be elegantly done like this:

mockGoferScript('My Script', {
  resultFromFM: () => import("./mocks/data.json"),
  logParams: true,
  delay: 500,
});

So with that out of the way, I'm feeling pretty comfortable with the current design and close to merging.