You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
1.1 KiB
JavaScript
46 lines
1.1 KiB
JavaScript
6 years ago
|
"use strict";
|
||
|
|
||
|
const expect = require("chai").expect;
|
||
|
|
||
|
const dm = require("../src");
|
||
|
|
||
|
let User = dm.createType("User", {
|
||
|
messages: dm.arrayOf(dm.string())
|
||
|
});
|
||
|
|
||
|
describe("guarded arrays", () => {
|
||
|
let joe = User({
|
||
|
messages: []
|
||
|
});
|
||
|
|
||
|
it("should accept valid values", () => {
|
||
|
joe.messages.push("Hello world!");
|
||
|
expect(joe.messages[0]).to.equal("Hello world!");
|
||
|
|
||
|
joe.messages.splice(0, 0, "Hello earth!")
|
||
|
expect(joe.messages[0]).to.equal("Hello earth!");
|
||
|
expect(joe.messages[1]).to.equal("Hello world!");
|
||
|
|
||
|
joe.messages[0] = "Hello moon!";
|
||
|
expect(joe.messages[0]).to.equal("Hello moon!");
|
||
|
expect(joe.messages[1]).to.equal("Hello world!");
|
||
|
});
|
||
|
|
||
|
it("should reject invalid values", () => {
|
||
|
expect(() => {
|
||
|
joe.messages.push(42);
|
||
|
}).to.throw("Expected a string, got a number instead");
|
||
|
|
||
|
expect(() => {
|
||
|
joe.messages.splice(0, 0, 43);
|
||
|
}).to.throw("Expected a string, got a number instead");
|
||
|
|
||
|
expect(() => {
|
||
|
joe.messages[0] = 44;
|
||
|
}).to.throw("Expected a string, got a number instead");
|
||
|
|
||
|
expect(joe.messages[0]).to.equal("Hello moon!");
|
||
|
expect(joe.messages[1]).to.equal("Hello world!");
|
||
|
});
|
||
|
});
|