|
1 |
| -import { describe, expect, it, vi, afterEach, beforeEach } from "vitest"; |
2 |
| -import mock from "mock-fs"; |
| 1 | +import { describe, expect, it, afterEach } from "vitest"; |
3 | 2 | import { generateDocumentation } from "../generateDocs";
|
4 | 3 | import path from "path";
|
| 4 | +import { rimraf } from "rimraf"; |
| 5 | +import { readdir } from "fs/promises"; |
| 6 | +import { existsSync, readFileSync } from "fs"; |
5 | 7 |
|
6 |
| -const mockFileSystem = { |
7 |
| - "mock/contracts/directory": { |
8 |
| - "testContract.ts": mock.load( |
9 |
| - path.join( |
10 |
| - process.cwd(), |
11 |
| - "/example-architecture/events/contracts/personRegisteredContractV1.ts" |
12 |
| - ) |
13 |
| - ), |
14 |
| - "testContract2.ts": mock.load( |
15 |
| - path.join( |
16 |
| - process.cwd(), |
17 |
| - "/example-architecture/events/contracts/personRegisteredContractV2.ts" |
18 |
| - ) |
19 |
| - ), |
20 |
| - }, |
21 |
| - "mock/events/directory": {}, |
22 |
| -}; |
| 8 | +const testContractsPath = path.join( |
| 9 | + process.cwd(), |
| 10 | + "/src/__tests__/fixtures/contracts" |
| 11 | +); |
| 12 | +const testDocsPath = path.join(process.cwd(), "/src/__tests__/fixtures/docs"); |
23 | 13 |
|
24 | 14 | describe("Create documentation for contract types, standardised for use with event catalog", () => {
|
25 |
| - beforeEach(function () { |
26 |
| - mock(mockFileSystem); |
| 15 | + afterEach(async () => { |
| 16 | + const files = await readdir(testDocsPath); |
| 17 | + for (const file of files) { |
| 18 | + await rimraf(path.join(testDocsPath, file)); |
| 19 | + } |
27 | 20 | });
|
28 |
| - afterEach(mock.restore); |
29 | 21 |
|
30 |
| - it("Creates documentation without error when passed valid contract types", () => { |
31 |
| - generateDocumentation("mock/contracts/directory", "mock/events/directory"); |
32 |
| - expect(true); |
| 22 | + it("Creates documentation without error when passed valid contract types", async () => { |
| 23 | + await generateDocumentation(testContractsPath, testDocsPath); |
| 24 | + var savedVersionedDocs = readFileSync( |
| 25 | + path.join( |
| 26 | + testDocsPath, |
| 27 | + "/PersonRegisteredContract/versioned/1/schema.json" |
| 28 | + ), |
| 29 | + { |
| 30 | + encoding: "utf8", |
| 31 | + } |
| 32 | + ); |
| 33 | + expect(savedVersionedDocs).toMatch(/("const": "PersonRegisteredContract")/); |
| 34 | + expect(savedVersionedDocs).toMatch(/("const": 1)/); |
| 35 | + expect( |
| 36 | + existsSync( |
| 37 | + path.join( |
| 38 | + testDocsPath, |
| 39 | + "/PersonRegisteredContract/versioned/1/index.md" |
| 40 | + ) |
| 41 | + ) |
| 42 | + ).toStrictEqual(true); |
| 43 | + var savedUnversionedDocs = readFileSync( |
| 44 | + path.join(testDocsPath, "/PersonRegisteredContract/schema.json"), |
| 45 | + { |
| 46 | + encoding: "utf8", |
| 47 | + } |
| 48 | + ); |
| 49 | + expect(savedUnversionedDocs).toMatch( |
| 50 | + /("const": "PersonRegisteredContract")/i |
| 51 | + ); |
| 52 | + expect(savedUnversionedDocs).toMatch(/("const": 2)/i); |
| 53 | + expect( |
| 54 | + existsSync(path.join(testDocsPath, "/PersonRegisteredContract/index.md")) |
| 55 | + ).toStrictEqual(true); |
33 | 56 | });
|
34 | 57 | });
|
0 commit comments