all files / test/unit/ body.es6

100% Statements 0/0
100% Branches 0/0
100% Functions 19/19
100% Lines 0/0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145                                                                                                                                                                                                                                                                                                 
import FormData from 'isomorphic-form-data';
import Body from '../../lib/body';
import { default as chai, expect } from 'chai';
import { default as chaiAsPromised } from 'chai-as-promised';
 
chai.use(chaiAsPromised);
 
describe('Body', () => {
 
  describe('constructor', () => {
 
    it('should set the _bodyText property to body if body is a string', () => {
 
      const testString = 'test';
 
      expect(new Body(testString))
        .to.have.property('_bodyText')
        .that.eqls(testString);
 
    });
 
    it('should set the _bodyFormData property to body if body is an instance of FormData', () => {
 
      const testFormData = new FormData();
 
      expect(new Body(testFormData))
        .to.have.property('_bodyFormData')
        .that.eqls(testFormData);
 
    });
 
    it('should set the _bodyText property to an empty string if body is undefined', () => {
 
      expect(new Body())
        .to.have.property('_bodyText')
        .that.eqls('');
 
    });
 
    it('should throw an Error if none of the conditions are met', () => {
 
      expect(() => new Body(true))
        .to.throw(Error);
 
    });
 
  });
 
  describe('text', () => {
 
    it('should set this.bodyUsed to true if the body has not been used', () => {
 
      const testBody = new Body();
 
      return expect(testBody.text())
        .to.be.fulfilled.then(() => {
 
          expect(testBody)
            .have.property('bodyUsed')
            .that.eqls(true);
 
        });
 
    });
 
    it('should return a resolved promise if the body has not been used', () => {
 
      const testBody = new Body();
 
      return expect(testBody.text())
        .to.eventually
        .eql('');
 
    });
 
    it('should return a rejected promise with a TypeError if the body has been used', () => {
 
      const testBody = new Body();
 
      return expect(testBody.text().then(() => testBody.text()))
        .to.eventually
        .be.rejectedWith(TypeError);
 
    });
 
  });
 
  describe('formData', () => {
 
    it('should return an instance of FormData', () => {
 
      const testBody = new Body('key1=val1&key2=val2');
 
      return expect(testBody.formData())
        .to.eventually
        .be.an.instanceOf(FormData);
 
    });
 
  });
 
  describe('json', () => {
 
    it('should parse json bodies', () => {
 
      const expectedBody = { key: 'value' };
      const testBody = new Body(JSON.stringify(expectedBody));
 
      return expect(testBody.json())
        .to.eventually
        .eql(expectedBody);
 
    });
 
  });
 
  describe('blob', () => {
 
    it('should return a rejected promise', () => {
 
      const testBody = new Body();
 
      return expect(testBody.blob())
        .to.eventually
        .be.rejectedWith(Error, /Method has not been implemented/);
 
    });
 
  });
 
  describe('arrayBuffer', () => {
 
    it('should return a rejected promise', () => {
 
      const testBody = new Body();
 
      return expect(testBody.arrayBuffer())
        .to.eventually
        .be.rejectedWith(Error, /Method has not been implemented/);
 
    });
 
  });
 
});