|
| 1 | +'use strict' |
| 2 | +const { describe, it, beforeEach, before } = require('mocha') |
| 3 | +const { expect } = require('chai') |
| 4 | +const { initState, offlinePeerCount, buildWebuiURLString } = require('../../../add-on/src/lib/state') |
| 5 | +const { optionDefaults } = require('../../../add-on/src/lib/options') |
| 6 | +const { URL } = require('url') |
| 7 | + |
| 8 | +describe('state.js', function () { |
| 9 | + describe('initState', function () { |
| 10 | + before(function () { |
| 11 | + global.URL = URL |
| 12 | + }) |
| 13 | + it('should copy passed options as-is', () => { |
| 14 | + const expectedProps = Object.assign({}, optionDefaults) |
| 15 | + delete expectedProps.publicGatewayUrl |
| 16 | + delete expectedProps.useCustomGateway |
| 17 | + delete expectedProps.ipfsApiUrl |
| 18 | + delete expectedProps.customGatewayUrl |
| 19 | + const state = initState(optionDefaults) |
| 20 | + for (const prop in expectedProps) { |
| 21 | + expect(state).to.have.property(prop, optionDefaults[prop]) |
| 22 | + } |
| 23 | + }) |
| 24 | + it('should generate pubGwURL*', () => { |
| 25 | + const state = initState(optionDefaults) |
| 26 | + expect(state).to.not.have.property('publicGatewayUrl') |
| 27 | + expect(state).to.have.property('pubGwURL') |
| 28 | + expect(state).to.have.property('pubGwURLString') |
| 29 | + }) |
| 30 | + it('should generate redirect state', () => { |
| 31 | + const state = initState(optionDefaults) |
| 32 | + expect(state).to.not.have.property('useCustomGateway') |
| 33 | + expect(state).to.have.property('redirect') |
| 34 | + }) |
| 35 | + it('should generate apiURL*', () => { |
| 36 | + const state = initState(optionDefaults) |
| 37 | + expect(state).to.not.have.property('ipfsApiUrl') |
| 38 | + expect(state).to.have.property('apiURL') |
| 39 | + expect(state).to.have.property('apiURLString') |
| 40 | + }) |
| 41 | + it('should generate gwURL*', () => { |
| 42 | + const state = initState(optionDefaults) |
| 43 | + expect(state).to.not.have.property('customGatewayUrl') |
| 44 | + expect(state).to.have.property('gwURL') |
| 45 | + expect(state).to.have.property('gwURLString') |
| 46 | + }) |
| 47 | + it('should generate webuiURLString', () => { |
| 48 | + const state = initState(optionDefaults) |
| 49 | + expect(state).to.have.property('webuiURLString') |
| 50 | + }) |
| 51 | + }) |
| 52 | + |
| 53 | + describe('offlinePeerCount', function () { |
| 54 | + it('should be equal -1', () => { |
| 55 | + expect(offlinePeerCount).to.be.equal(-1) |
| 56 | + }) |
| 57 | + }) |
| 58 | + |
| 59 | + describe('buildWebuiURLString', function () { |
| 60 | + let fakeState |
| 61 | + beforeEach(() => { |
| 62 | + fakeState = { apiURLString: 'http://127.0.0.1:5001/' } |
| 63 | + }) |
| 64 | + it('should be throw error on missing apiURLString', () => { |
| 65 | + expect(() => buildWebuiURLString({})).to.throw('Missing apiURLString') |
| 66 | + }) |
| 67 | + it('should return /webui for optionDefaults', () => { |
| 68 | + fakeState.webuiFromDNSLink = optionDefaults.webuiFromDNSLink |
| 69 | + expect(buildWebuiURLString(fakeState)).to.be.equal(`${fakeState.apiURLString}webui/`) |
| 70 | + }) |
| 71 | + it('should return /webui when webuiFromDNSLink is falsy', () => { |
| 72 | + fakeState.webuiFromDNSLink = undefined |
| 73 | + expect(buildWebuiURLString(fakeState)).to.be.equal(`${fakeState.apiURLString}webui/`) |
| 74 | + }) |
| 75 | + it('should return /ipns/webui.ipfs.io when webuiFromDNSLink is true', () => { |
| 76 | + fakeState.webuiFromDNSLink = true |
| 77 | + expect(buildWebuiURLString(fakeState)).to.be.equal(`${fakeState.apiURLString}ipns/webui.ipfs.io/`) |
| 78 | + }) |
| 79 | + }) |
| 80 | +}) |
0 commit comments