Skip to content

Instantly share code, notes, and snippets.

@mfdj
Last active January 17, 2020 21:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mfdj/a7a68317aa28747389105bc888c930dd to your computer and use it in GitHub Desktop.
Save mfdj/a7a68317aa28747389105bc888c930dd to your computer and use it in GitHub Desktop.
mocking-required-code-in-jasmine
export const log = function() {
return "ghost-dog";
};
export const blob = function() {
return "blee-bloop";
};
/*
// will work the same as this
export default Object.seal({
log() {
return "ghost-dog";
},
blob() {
return "blee-bloop";
}
});
*/
import { log } from "lib/ghost_dog";
export const proxied = function() {
return log();
};
export const assigned = log;
/*
// will work the same as this
import * as GhostDog from "lib/ghost_dog";
export const proxied = function() {
return GhostDog.log();
};
export const assigned = GhostDog.log;
*/
/*
// will work the same as this
const GhostDog = require("lib/ghost_dog");
export default Object.seal({
proxied() {
return GhostDog.log();
},
assigned: GhostDog.log,
});
*/
import { log } from "lib/ghost_dog";
import { assigned, proxied } from "lib/samurai";
// import * as Samurai from "lib/samurai";
describe("Samurai", function() {
// (1) - - - - - - - - - - - - - - - - - - - - - - - -
it("GhostDog.log returns ghost-dog", function() {
expect(
log()
).toEqual("ghost-dog");
});
it("Samurai.proxied returns ghost-dog", function() {
expect(
//- Samurai.proxied()
proxied()
).toEqual("ghost-dog");
});
it("Samurai.assigned returns ghost-dog", function() {
expect(
//- Samurai.assigned()
assigned()
).toEqual("ghost-dog");
});
// (2) - - - - - - - - - - - - - - - - - - - - - - - -
describe("spied-on log", function() {
beforeEach(function () {
spyOn(
require("lib/ghost_dog"), "log"
).andReturn("hola-taco");
});
it("GhostDog.log returns hola-taco", function() {
expect(
log()
).toEqual("hola-taco");
});
it("Samurai.proxied returns hola-taco", function() {
expect(
//- Samurai.proxied()
proxied()
).toEqual("hola-taco");
});
it("Samurai.assigned returns ghost-dog", function() {
expect(
//- Samurai.assigned()
assigned()
).toEqual("ghost-dog");
});
// (3) - - - - - - - - - - - - - - - - - - - - - - - -
describe("nested spie", function() {
beforeEach(function () {
spyOn(
require("lib/ghost_dog"), "blob" // <- if we try to spy-on log here jasmine will shit the bed
).andReturn("frosting-salad");
});
it("GhostDog.log returns hola-taco", function() {
expect(
log()
).toEqual("hola-taco");
});
it("Samurai.proxied returns hola-taco", function() {
expect(
//- Samurai.proxied()
proxied()
).toEqual("hola-taco");
});
it("Samurai.assigned returns ghost-dog", function() {
expect(
//- Samurai.assigned()
assigned()
).toEqual("ghost-dog");
});
});
});
// (4) - - - - - - - - - - - - - - - - - - - - - - - -
describe("adjacent spie", function() {
beforeEach(function () {
spyOn(
require("lib/ghost_dog"), "log"
).andReturn("migrant-food");
});
it("GhostDog.log returns migrant-food", function() {
expect(
log()
).toEqual("migrant-food");
});
it("Samurai.proxied returns migrant-food", function() {
expect(
//- Samurai.proxied()
proxied()
).toEqual("migrant-food");
});
it("Samurai.assigned returns ghost-dog", function() {
expect(
//- Samurai.assigned()
assigned()
).toEqual("ghost-dog");
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment