{"size": 3142, "ext": "js", "max_stars_repo_path": "test/lib/registry.test.js", "max_stars_repo_name": "dsafio/dsafio", "max_stars_repo_stars_event_min_datetime": "2017-08-25T22:14:29.000Z", "max_stars_repo_stars_event_max_datetime": "2020-11-09T15:05:20.000Z", "max_issues_repo_path": "test/lib/registry.test.js", "max_issues_repo_name": "dsafio/dsafio", "max_issues_repo_issues_event_min_datetime": "2017-08-25T15:23:39.000Z", "max_issues_repo_issues_event_max_datetime": "2019-08-30T09:00:29.000Z", "max_forks_repo_path": "test/lib/registry.test.js", "max_forks_repo_name": "dsafio/dsafio", "max_forks_repo_forks_event_min_datetime": "2017-09-07T22:29:49.000Z", "max_forks_repo_forks_event_max_datetime": "2018-02-26T02:40:48.000Z"}
{"max_stars_count": 16, "max_issues_count": 35, "max_forks_count": 9, "avg_line_length": 25.7540983607, "max_line_length": 80, "alphanum_fraction": 0.5073201782}
/*!
 * Dsafio
 * Copyright (c) 2017 Joel Wallis Jucá <joelwallis@gmail.com>
 * MIT Licensed
 */

const axios = require("axios");
const chai = require("chai");
const sinon = require("sinon");
const fs = require("../../lib/fs-as-promise");
const registry = require("../../lib/registry");

chai.use(require("chai-as-promised"));
const expect = chai.expect;

describe("lib/registry", function() {
  beforeEach(() => {
    sinon
      .stub(fs, "readFile")
      .returns(Promise.resolve(JSON.stringify({ foo: "foo" })));

    sinon.stub(fs, "writeFile").returns(Promise.resolve());

    sinon.stub(axios, "get").returns(
      Promise.resolve({
        data: {
          content: Buffer.from('{"foo":"foo"}', "utf8").toString("base64")
        }
      })
    );
  });

  afterEach(() => {
    fs.readFile.restore();
    fs.writeFile.restore();
    axios.get.restore();
  });

  it("is an object", () => {
    expect(registry).to.be.an("object");
  });

  describe("get()", function() {
    it("is a function", () => expect(registry.get).to.be.a("function"));

    it("accepts an array as argument", () => {
      return Promise.all([
        expect(registry.get()).to.be.fulfilled,
        expect(registry.get(["foo"])).to.be.fulfilled
      ]);
    });

    it("rejects when passed argument other than array", () => {
      return Promise.all(
        ["foo", true, 123, null, [], {}].map(value => {
          return expect(registry.get(value)).to.be.rejected;
        })
      );
    });

    it("returns a promise", function() {
      const promises = [];

      expect(
        (() => {
          promises.push(registry.get());
          return promises.slice(-1)[0];
        })()
      ).to.be.a("promise");

      expect(
        (() => {
          promises.push(registry.get(["foo", "bar"]));
          return promises.slice(-1)[0];
        })()
      ).to.be.a("promise");
    });
  });

  describe("update()", function() {
    it("is a function", () => expect(registry.update).to.be.a("function"));

    it("returns a promise", () => expect(registry.update()).to.be.a("promise"));

    it("updates the registry", function() {
      return registry
        .get()
        .then(entries => {
          expect(entries).to.be.an("object");
          expect(entries.foo).to.be.a("string");
          expect(entries.foo).to.equal("foo");
        })
        .then(() => {
          fs.readFile.restore();
          axios.get.restore();

          sinon
            .stub(fs, "readFile")
            .callsFake(() => Promise.reject(new Error()));

          sinon.stub(axios, "get").returns(
            Promise.resolve({
              data: {
                content: Buffer.from('{"updated": true}', "utf8").toString(
                  "base64"
                )
              }
            })
          );
        })
        .then(registry.update)
        .then(() => registry.get())
        .then(entries => {
          expect(entries).to.be.an("object");
          expect(entries.foo).to.equal(undefined);
          expect(entries.updated).to.be.a("boolean");
          expect(entries.updated).to.equal(true);
        });
    });
  });
});
