Giter VIP home page Giter VIP logo

mgwdev-spfx-utfx's Introduction

mgwdev-spfx-utfx

Link to the repo

This library will help You with writing unit tests for SPFx solutions. There are three supported configuration You can use

  1. integration - which uses credentials provided in config to connect with SharePoint and uses the connection to communicate with SharePoint whenever context.spHttpClient is used.
  2. integrationWithFileGeneration - same as 1) but also generated file with responses in provided path (second argument of MockHttpClientFactory.setupContextFromConfig).
  3. isolated - tests will not communicate with Your tenant. Only data provided in file will be used.

How to use it

The scenario I had in mind is following. You write Your test in integraation mode Write the logic Test passess Switch to isolated mode You have Your unit test!

Installation

npm install mgwdev-spfx-utfx --save-dev

Simpliest setup

Here is the simpliest possible test You can write. Of course You can inject context anywhere You will need web part or extension context.

/// <reference types="jest" />

import { assert } from "chai";
import { MockHttpClientFactory, TestConfiguration, IMockSPHttpClient } from "mgwdev-spfx-utfx";

jest.mock("@microsoft/sp-http", () => {
    return {
        SPHttpClient: {
            configurations: {
                v1: 1
            }
        }
    }
});
let context;
describe("Test UTFX", () => {
    beforeAll(() => {
        let mockHttpClientFactory = new MockHttpClientFactory();
        context = mockHttpClientFactory.setupContextFromConfig({
            authenticationConfiguration: {
              username: "admin@<tenant_name>.onmicrosoft.com",
              password: "<password>", 
              online: true
            },
            siteUrl : "https://<tenant_name>.sharepoint.com",
            runConfiguration: TestConfiguration.integrationWithFileGeneration
        }, "./tests/responses/Web.json");
        return context.spHttpClient.init();
    });
    test("getWebInfo (absolute)", async () => {
        let response = await context.spHttpClient.get("https://<tenant_name>.sharepoint.com/sites/tea-point/_api/web?$select=Title");
        let result = await response.json();
        assert.equal(result.Title, "Tea Point - English");
    });
    test("getWebInfo (relative)", async () => {
        let response = await context.spHttpClient.get("/sites/tea-point/_api/web?$select=Title");
        let result = await response.json();
        assert.equal(result.Title, "Tea Point - English");
    });
    afterAll(async() => {
        await context.spHttpClient.dispose();
    });
});

mgwdev-spfx-utfx's People

Contributors

mgwojciech avatar

Stargazers

 avatar  avatar

Watchers

 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.