|
| 1 | +import { DataFactory, Parser, Store } from "n3" |
| 2 | +import assert from "node:assert" |
| 3 | +import { describe, it } from "node:test" |
| 4 | + |
| 5 | +import { Telephone } from "@solid/object"; |
| 6 | + |
| 7 | +describe("Telephone tests", () => { |
| 8 | + |
| 9 | + const sampleRDF = ` |
| 10 | +@prefix vcard: <http://www.w3.org/2006/vcard/ns#> . |
| 11 | +
|
| 12 | +<https://example.org/person/1> |
| 13 | + a vcard:Individual ; |
| 14 | + vcard:fn "Alice" ; |
| 15 | + vcard:hasTelephone <https://example.org/phone/1> . |
| 16 | +
|
| 17 | +<https://example.org/phone/1> |
| 18 | + a vcard:Telephone ; |
| 19 | + vcard:hasValue "+1234567890" ; |
| 20 | + vcard:TelephoneType vcard:Cell . |
| 21 | +`; |
| 22 | + |
| 23 | + it("should parse and retrieve phone number", () => { |
| 24 | + const store = new Store() |
| 25 | + store.addQuads(new Parser().parse(sampleRDF)) |
| 26 | + |
| 27 | + const telephone = new Telephone( |
| 28 | + DataFactory.namedNode("https://example.org/phone/1"), |
| 29 | + store, |
| 30 | + DataFactory |
| 31 | + ) |
| 32 | + |
| 33 | + assert.equal(telephone.phoneNumber, "+1234567890") |
| 34 | + assert.equal(typeof telephone.phoneNumber, "string") |
| 35 | + }) |
| 36 | + |
| 37 | + it("should allow setting phone number", () => { |
| 38 | + const store = new Store() |
| 39 | + store.addQuads(new Parser().parse(sampleRDF)) |
| 40 | + |
| 41 | + const telephone = new Telephone( |
| 42 | + DataFactory.namedNode("https://example.org/phone/1"), |
| 43 | + store, |
| 44 | + DataFactory |
| 45 | + ) |
| 46 | + |
| 47 | + telephone.phoneNumber = "+0987654321" |
| 48 | + |
| 49 | + assert.equal(telephone.phoneNumber, "+0987654321") |
| 50 | + }) |
| 51 | + |
| 52 | + it("should parse and retrieve phone type", () => { |
| 53 | + const store = new Store() |
| 54 | + store.addQuads(new Parser().parse(sampleRDF)) |
| 55 | + |
| 56 | + const telephone = new Telephone( |
| 57 | + DataFactory.namedNode("https://example.org/phone/1"), |
| 58 | + store, |
| 59 | + DataFactory |
| 60 | + ) |
| 61 | + |
| 62 | + const phoneType = telephone.phoneType |
| 63 | + |
| 64 | + assert.ok(phoneType !== undefined) |
| 65 | + assert.equal(typeof phoneType, "string") |
| 66 | + assert.equal(phoneType, "http://www.w3.org/2006/vcard/ns#Cell") |
| 67 | + }) |
| 68 | + |
| 69 | + it("should allow setting phone type", () => { |
| 70 | + const store = new Store() |
| 71 | + |
| 72 | + const telephone = new Telephone( |
| 73 | + DataFactory.namedNode("https://example.org/phone/2"), |
| 74 | + store, |
| 75 | + DataFactory |
| 76 | + ) |
| 77 | + |
| 78 | + telephone.phoneNumber = "+1112223333" |
| 79 | + telephone.phoneType = "http://www.w3.org/2006/vcard/ns#Car" |
| 80 | + |
| 81 | + assert.equal(telephone.phoneType, "http://www.w3.org/2006/vcard/ns#Car") |
| 82 | + }) |
| 83 | + |
| 84 | + it("should throw when phone number is missing", () => { |
| 85 | + const noPhoneRDF = ` |
| 86 | +@prefix vcard: <http://www.w3.org/2006/vcard/ns#> . |
| 87 | +
|
| 88 | +<https://example.org/phone/empty> |
| 89 | + a vcard:Telephone . |
| 90 | +` |
| 91 | + const store = new Store() |
| 92 | + store.addQuads(new Parser().parse(noPhoneRDF)) |
| 93 | + |
| 94 | + const telephone = new Telephone( |
| 95 | + DataFactory.namedNode("https://example.org/phone/empty"), |
| 96 | + store, |
| 97 | + DataFactory |
| 98 | + ) |
| 99 | + |
| 100 | + assert.throws(() => { |
| 101 | + telephone.phoneNumber |
| 102 | + }) |
| 103 | + }) |
| 104 | + |
| 105 | +}) |
0 commit comments