Giter Club home page Giter Club logo

Comments (6)

alexilyaev avatar alexilyaev commented on May 19, 2024

@ilyagelman @morsdyce Should work just fine, add this to any page to test (ES6 must be supported):

/**
 * Shredder API Dummy
 */

window.Shredder = {
  sayHi: (one, two) => {
    console.log('API sayHi', one, two);
  }
};

/**
 * Add an iFrame element for the Shredder UI
 */

let iframeElm = document.createElement('iframe');
iframeElm.src = '';
iframeElm.width = '100%';
iframeElm.height = '100%';

document.body.appendChild(iframeElm);

/**
 * Get the iFrame's document, body and head
 */

let iframeRoot = iframeElm.contentDocument;
let iframeBody = iframeRoot.body || iframeRoot.querySelector('body');
let iframeHead = iframeRoot.querySelector('head') || iframeBody;

/**
 * Prepare Shredder UI Styles
 */

let uiStyle = document.createElement('style');
uiStyle.textContent = `
  h1 {
    background: lightblue;
  }
`;

/**
 * Prepare Shredder UI View
 */

let uiHtml = document.createElement('div');
uiHtml.innerHTML = `
  <div>
    <h1>Bad Ass API Mocking</h1>
  </div>
`;

/**
 * Prepare Shredder UI Code
 */

let uiScript = document.createElement('script');
uiScript.type = 'text/javascript';
uiScript.text = `
  // Execute methods from the Shredder API
  window.parent.Shredder.sayHi('Foo', 'Bar')
`;

/**
 * Inject the Shredder UI
 */

iframeBody.appendChild(uiHtml);
iframeHead.appendChild(uiStyle);
iframeBody.appendChild(uiScript);

Open questions

  • The code and styles must be post transpilation, that's no issue on production, we could have them as strings and just inject them, but how would it work in development?
  • We would still need some CSS to position the iframe and handle showing/hiding it, this would have to be added to the original page, the iframe styles would have to be as explicit as possible to avoid external styles from affecting it.

from mimic.

ilyagelman avatar ilyagelman commented on May 19, 2024

@alexilyaev What about mocking xhr requests within iframe?

from mimic.

alexilyaev avatar alexilyaev commented on May 19, 2024

@ilyagelman You don't, you mock them outside as usual, the iFrame is just the carrier that will communicate with the Shredder API.

from mimic.

alexilyaev avatar alexilyaev commented on May 19, 2024

Maayan wrote:
Please keep in mind for whoever implements the iframe, you need to test it in both loading scenarios.

  1. Global var via script injection
  2. Common js import (no global var is created at this method currently)

from mimic.

morsdyce avatar morsdyce commented on May 19, 2024

+1 for including this here.

On Wed, Dec 2, 2015 at 7:47 PM, Alex Ilyaev [email protected]
wrote:

Maayan wrote:
Please keep in mind for whoever implements the iframe, you need to test it
in both loading scenarios.

  1. Global var via script injection
  2. Common js import (no global var is created at this method currently)


Reply to this email directly or view it on GitHub
#22 (comment).

from mimic.

alexilyaev avatar alexilyaev commented on May 19, 2024

@ilyagelman Does it work with the 2 points Maayan mentioned?

from mimic.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.