|
1 | 1 | import assert from 'node:assert' |
2 | 2 | import {describe, it} from 'mocha' |
3 | 3 | import xpath from 'xpath' |
4 | | -import {convertSyllabusXMLtoMODS} from './syllabus.js' |
| 4 | +import {addRoleTerm, convertSyllabusXMLtoMODS} from './syllabus.js' |
| 5 | +import {DOMParser} from '@xmldom/xmldom' |
5 | 6 |
|
6 | 7 | // helper function to wrap XML in a root </xml> element |
7 | 8 | const x = (xml) => `<xml>${xml}</xml>` |
8 | 9 |
|
| 10 | +describe('addRoleTerm', () => { |
| 11 | + const authority = 'marcrelator' |
| 12 | + const authorityURI = 'http://id.loc.gov/vocabulary/relators' |
| 13 | + |
| 14 | + it('should add a role/roleTerm element with authority and type attributes', async () => { |
| 15 | + const doc = new DOMParser().parseFromString('<mods></mods>', 'text/xml') |
| 16 | + const valueURI = 'http://id.loc.gov/vocabulary/relators/tch' |
| 17 | + addRoleTerm(doc.documentElement, 'teacher', valueURI) |
| 18 | + const roleTerm = xpath.select1('//mods/role/roleTerm', doc) |
| 19 | + assert.ok(roleTerm) |
| 20 | + assert.strictEqual(roleTerm.textContent, 'teacher') |
| 21 | + assert.strictEqual(roleTerm.getAttribute('authority'), authority) |
| 22 | + assert.strictEqual(roleTerm.getAttribute('authorityURI'), authorityURI) |
| 23 | + assert.strictEqual(roleTerm.getAttribute('valueURI'), valueURI) |
| 24 | + }) |
| 25 | + |
| 26 | + it('should work without a valueURI', async () => { |
| 27 | + const doc = new DOMParser().parseFromString('<mods></mods>', 'text/xml') |
| 28 | + addRoleTerm(doc.documentElement, 'teacher') |
| 29 | + const roleTerm = xpath.select1('//mods/role/roleTerm', doc) |
| 30 | + assert.ok(roleTerm) |
| 31 | + assert.strictEqual(roleTerm.textContent, 'teacher') |
| 32 | + assert.strictEqual(roleTerm.getAttribute('authority'), authority) |
| 33 | + assert.strictEqual(roleTerm.getAttribute('authorityURI'), authorityURI) |
| 34 | + }) |
| 35 | +}) |
| 36 | + |
| 37 | +describe('personalNames', () => { |
| 38 | + it('should add a single personal name from local/faculty elements', async () => { |
| 39 | + const inputXML = x(`<local><courseInfo><faculty>John Doe</faculty></courseInfo></local>`) |
| 40 | + const result = convertSyllabusXMLtoMODS(inputXML) |
| 41 | + const nameElement = xpath.select1('//mods/name[@type="personal"]', result) |
| 42 | + assert.ok(nameElement) |
| 43 | + const namePart = xpath.select1('namePart', nameElement) |
| 44 | + assert.ok(namePart) |
| 45 | + assert.strictEqual(namePart.textContent, 'John Doe') |
| 46 | + }) |
| 47 | + |
| 48 | + it('should add multiple personal names from local/faculty elements', async () => { |
| 49 | + const inputXML = x(`<local><courseInfo><faculty>John Doe, Jane Smith</faculty></courseInfo></local>`) |
| 50 | + const result = convertSyllabusXMLtoMODS(inputXML) |
| 51 | + const nameElements = xpath.select('//mods/name[@type="personal"]', result) |
| 52 | + assert.strictEqual(nameElements.length, 2) |
| 53 | + const namePart1 = xpath.select1('namePart', nameElements[0]) |
| 54 | + assert.ok(namePart1) |
| 55 | + assert.strictEqual(namePart1.textContent, 'John Doe') |
| 56 | + const namePart2 = xpath.select1('namePart', nameElements[1]) |
| 57 | + assert.ok(namePart2) |
| 58 | + assert.strictEqual(namePart2.textContent, 'Jane Smith') |
| 59 | + }) |
| 60 | +}) |
| 61 | + |
9 | 62 | describe('addIdentifiers', () => { |
10 | 63 | it('should add course number and section identifiers', async () => { |
11 | 64 | const courseNumber = 'ARTED-101' |
|
0 commit comments