npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fm-mock

v1.5.0

Published

A framework for mocking the FileMaker webviewer window object for developing and testing fm webviewer apps in browser.

Downloads

110

Readme

fm-mock

WebViewer-less development for WebViewers

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

This can be especially useful if you're developing in a frontend framework like React, Vue, Svelte etc and you want to use dev tools in your browser of choice.

Try

Open ./example/index.html in your browser. The javascript in this file calls FileMaker.PerformScript and successfully gets data despite running outside of a webviewer.

Open ./example/FMMock.fmp12. This webviewer runs the same html code, but index.css has been inlined in a <style> tag and the last <script> block (the one that loads the mock scripts) has been disabled. With that code disabled, the JS uses the default FileMaker.PerformScript and calls real FM scripts.

Run npm run example-multi to see how it works in a multi-file environment. Look at ./exampl/multi-file/* to see how this works.

Usage

Install / Import

npm install --save-dev fm-mock

Import ES Module (preferred):

import { mockScript } from 'fm-mock';

Other options

Via script tag:

<script src="path/to/fm-mock.js"></script>

CommonJS require:

const FMMock = require('fm-mock');

Use

Once the code is imported, mocking scripts will immediately replace the window.FileMaker object and the script will be ready to call.

// mock some scripts
FMMock.mockScript('Create Record', () => {
    const res = JSON.stringify({"newRecordID": 123});
    // mock scripts should call global functions, just like FM must
    window.addRecordToUI(res);
});
FMMock.mockScript('Delete Record', () => { ... });

// now call your scripts like this
window.FileMaker.PerformScript('Create Record', param);
window.FileMaker.PerformScriptWithOption('Create Record', param, opt);

FMGofer Integration

If you're using FMGofer, then it's even easier to mock scripts. Use mockGoferScript instead of mockScript.

import { mockGoferScript } from 'fm-mock';

// can return a value directly!
// string, number, boolean, object, array, will all be returned as a string just
// like FM's `Perform JavaScript In Web Viewer` step does
mockGoferScript('Get Count', {
  resultFromFM: 17,
});

// can pass a function to dynamically generate the return value, like mockScript
mockGoferScript('Get Count', {
  resultFromFM: () => Math.floor(Math.random() * 100),
});
// async works too
mockGoferScript('Get Count', {
  resultFromFM: async () => {
    const res = await fetch('https://api.example.com/count');
    return await res.text();
  },
});

// store big json in a separate file
mockGoferScript('Get Data', {
  resultFromFM: () => import('./mocks/data.json').then((res) => res.default),
});

// you can dynamically simulate filemaker errors by throwing an error
mockGoferScript('Get Data', {
  resultFromFM: (param) => {
    switch (param.action) {
      case 'GET_CUSTOMER':
        return { name: 'John Doe' };
      case 'GET_ORDER':
        return { order: '123' };
      default:
        throw new Error(`Unknown action: ${param.action}`);
    }
  },
});

// convenient options to simulate different situations like slow scripts and
// errors that occur in your FM script (like a record lock conflict)
mockGoferScript('Get Data', {
  resultFromFM: 'this might be an error',
  // simulate 2s fm script
  delay: 2000,
  // simulate 20% chance of error (FMGofer.PerformScript will reject)
  // this option is ignored if resultFromFM is a function that throws an error
  returnError: Math.random() > 0.8,
  // logs callbackName, promiseID, parameter as would be passed to FM
  logParams: true,
});

Vite

If you're using Vite, toggling dev/production is easy. Use an if statement to only mock scripts in development.

import { mockScript } from 'fm-mock';

if (import.meta.env.DEV) {
    mockScript('Fetch Records', (param) => { ... });
}

Restoring window.FileMaker

If you wish to restore the original FileMaker functions, you can. This can be useful if your app has automated tests and you want to restore FileMaker between tests.

import { mockScript, restoreMocks } from 'fm-mock';

restoreMocks();

Now npm run dev will let you test in the browser, and npm run build will create a version ready to use in your FM webviewer with fm-mock removed completely.

Test

npm test

Contribute

If you have any feature ideas or bug fixes, please let me know or send a pull request.