Git Product home page Git Product logo

dw-api-mock-1's Introduction

DW MOCK API


Introduction

This is a mock API or an API stub for the Demandware Script API, the main goal is to make unit testing easier so that not every single Class/Object needs to be stubbed out.

Example usage

It has been tested to work well in conjunction with mochajs as a testrunner and chai, sinon, sinon-chai and proxyquire as utility libs.

A unit test would then look something like

#!javascript
'use strict';

// jscs:disable jsDoc

var describe   = require('mocha').describe;
var it         = require('mocha').it;
var chai       = require("chai");
var sinon      = require("sinon");
var sinonChai  = require("sinon-chai");
var expect     = chai.expect;
var proxyquire = require('proxyquire').noCallThru();

chai.use(sinonChai);

require.extensions['.ds'] = require.extensions['.js'];
require('dw-api-mock/demandware-globals');

// add cartridges dir as module lookup location, the app-module-path package lets you do this nicely
require('app-module-path').addPath(process.cwd() + '/cartridges');

describe("Product Model", function() {

    it("should return a variation model", function() {
	    var ProductModel = require('app_storefront_controller/cartridge/scripts/models/ProductModel');
		
		var instance = new ProductModel(new dw.catalog.Product());
		
		expect(instance.getVariationModel()).to.exist.
	});
});

All the dependencies can be added to the root of your repository into a package.json file and the unit tests would be inside a test folder, so you would have

  • cartridges
  • site_import
  • test

Sample package.json

{
  "name": "my-repo",
  "version": "1.0.0",
  "description": "A repo description",
  "main": "index.js",
  "directories": {
    "test": "test"
  },
  "devDependencies": {
    "app-module-path": "^1.0.4",
    "chai": "^3.4.1",
    "dw-api-mock": "git+ssh://github.com/SalesforceCommerceCloud/dw-api-mock.git",
    "istanbul": "^0.4.1",
    "mocha": "^2.3.4",
    "proxyquire": "^1.7.3",
    "sinon": "^1.17.2",
    "sinon-chai": "^2.8.0",
    "xunit-file": "0.0.7"
  },
  "scripts": {
    "test": "mocha test/*",
    "coverage": "istanbul cover ./node_modules/mocha/bin/_mocha"
  },
  "keywords": [
    "myrepo"
  ],
  "author": "Your Name",
  "license": "UNLICENSED",
  "homepage": "www.yourhomepage.com"
}

Now you can simply run

#!bash
$ npm install
$ mocha test/*

License

Licensed under the current NDA and licensing agreement in place with your organization. (This is explicitly not open source licensing.)

dw-api-mock-1's People

Contributors

sgd1953 avatar hnestmann avatar ngeor avatar ezonno avatar challmannsf avatar jbachelet avatar tobiaslohr avatar elythingol avatar teneff avatar dmersiowsky avatar kkamenov1 avatar djb2c avatar dshestopalov avatar marcelblijleven avatar xiu-loreal avatar kdomachowski avatar svc-scm avatar tsvetelina-mladenova avatar wyand-sp avatar

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.