Skip to content

Commit

Permalink
test: created initial tests (with 100% coverage)
Browse files Browse the repository at this point in the history
Also moved the webservice-module to the correct location in the repository
  • Loading branch information
Luke Carr committed May 21, 2020
1 parent 217f66d commit 989f82b
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 5 deletions.
35 changes: 35 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"eslint-plugin-prettier": "^3.1.3",
"husky": "^4.2.5",
"jest": "^26.0.1",
"nock": "^12.0.3",
"npm-run-all": "^4.1.5",
"prettier": "^2.0.5",
"rimraf": "^3.0.2",
Expand Down
2 changes: 1 addition & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export abstract class Client {
this.token = options.token;

this.got = got.extend({
prefixUrl: `${this.baseURL}/webservice/rest/server.php`,
prefixUrl: this.baseURL,
searchParams: {
wstoken: this.token,
moodlewsrestformat: "json",
Expand Down
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Client, ClientOptions } from "./client";
import AuthModule from "./modules/auth";
import WebServiceModule from "./modules/webservice";
import CoreModule from "./modules/core";

/**
* The main Joodle client class. Used to make API calls to Moodle's Web Services
Expand All @@ -10,7 +10,7 @@ import WebServiceModule from "./modules/webservice";
export class Joodle extends Client {
public auth: AuthModule;

public webService: WebServiceModule;
public core: CoreModule;

/**
* Initializes a new Joodle client instance for making API calls to Moodle's
Expand All @@ -20,6 +20,6 @@ export class Joodle extends Client {
public constructor(options: ClientOptions) {
super(options);
this.auth = new AuthModule(this);
this.webService = new WebServiceModule(this);
this.core = new CoreModule(this);
}
}
15 changes: 15 additions & 0 deletions src/modules/core/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Module from "..";
import WebServiceModule from "./webservice";
import { Client } from "../../client";

/**
* Functions relating to core Moodle operations.
*/
export default class CoreModule extends Module {
public webservice: WebServiceModule;

public constructor(client: Client) {
super(client);
this.webservice = new WebServiceModule(client);
}
}
File renamed without changes.
2 changes: 1 addition & 1 deletion src/modules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default abstract class Module {
wsfunction: string,
searchParams?: any
): Promise<unknown> {
const { body } = await this.client.got.get("", {
const { body } = await this.client.got.get("webservice/rest/server.php", {
searchParams: {
wsfunction,
...searchParams,
Expand Down
48 changes: 48 additions & 0 deletions tests/joodle.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import nock from "nock";
import { Joodle } from "../src";

describe("The Joodle client class", () => {
const baseURL = "https://moodle.example.com/";
const token = "abc123";
let joodle: Joodle;

beforeAll(() => {
joodle = new Joodle({
baseURL,
token,
});

nock(baseURL)
.get(`/webservice/rest/server.php?wsfunction=auth_email_get_signup_settings&wstoken=${token}&moodlewsrestformat=json`)
.reply(200, {
namefields: [
"firstname",
"lastname",
],
passwordpolicy: "Password must exist!",
warnings: [],
});

nock(baseURL)
.get(`/webservice/rest/server.php?wsfunction=core_webservice_get_site_info&wstoken=${token}&moodlewsrestformat=json`)
.reply(200, {
errorcode: "invalidtoken",
exception: "moodle_exception",
message: "Invalid token - token not found",
});
});

it("should be initialized with a baseURL and token", () => {
expect(joodle.got.defaults.options.prefixUrl).toBe(baseURL);
expect(joodle.got.defaults.options.searchParams!.get("wstoken")).toBe(token);
expect(joodle.got.defaults.options.searchParams!.get("moodlewsrestformat")).toBe("json");
});

it("should make API calls using the got library", () => {
return expect(joodle.auth.email.getSignUpSettings()).resolves.toBeDefined();
});

it("should catch errors returned by Moodle", () => {
return expect(joodle.core.webservice.getSiteInfo()).rejects.toBeDefined();
});
});

0 comments on commit 989f82b

Please sign in to comment.