|
| 1 | +// tslint:disable: no-unused-expression |
| 2 | + |
| 3 | +import { Experiment } from '../models/experiment'; |
| 4 | +import { Identifier } from '../models/identifier'; |
| 5 | +import { Query } from '../models/query/query'; |
| 6 | +import { HttpRequest, HttpResponse, RestClient } from '../protocol/rest-client'; |
| 7 | +import { FixtureSet } from '../protocol/test-utils'; |
| 8 | +import { TspClient } from '../protocol/tsp-client'; |
| 9 | +import { JSONBigUtils } from './jsonbig-utils'; |
| 10 | + |
| 11 | +describe('JSONBigUtils tests', () => { |
| 12 | + |
| 13 | + const client = new TspClient('not-relevant'); |
| 14 | + const httpRequestMock = jest.fn<Promise<HttpResponse>, [req: HttpRequest]>(); |
| 15 | + |
| 16 | + let fixtures: FixtureSet; |
| 17 | + |
| 18 | + beforeAll(async () => { |
| 19 | + fixtures = await FixtureSet.fromFolder(__dirname, '../../fixtures/tsp-client'); |
| 20 | + RestClient['httpRequest'] = httpRequestMock; |
| 21 | + }); |
| 22 | + |
| 23 | + beforeEach(() => { |
| 24 | + httpRequestMock.mockReset(); |
| 25 | + httpRequestMock.mockResolvedValue({ text: '', status: 404, statusText: 'Not found' }); |
| 26 | + }); |
| 27 | + |
| 28 | + it('testJSONBigUtils-with-normalizer', async () => { |
| 29 | + httpRequestMock.mockReturnValueOnce(fixtures.asResponse('create-experiment-0.json')); |
| 30 | + const response = await client.createExperiment(new Query({})); |
| 31 | + const experiment = response.getModel()!; |
| 32 | + |
| 33 | + const input = JSONBigUtils.stringify(experiment); |
| 34 | + const experiment2: Experiment = JSONBigUtils.parse(input, Experiment); |
| 35 | + |
| 36 | + expect(experiment).toEqual(experiment2); |
| 37 | + }); |
| 38 | + |
| 39 | + it('testJSONBigUtils-no-normalizer', async () => { |
| 40 | + const inputObj = 'hallo'; |
| 41 | + const input = JSONBigUtils.stringify(inputObj); |
| 42 | + const output = JSONBigUtils.parse<string>(input); |
| 43 | + expect(typeof output).toEqual('string'); |
| 44 | + |
| 45 | + let bigIntObj = BigInt("1234567890123456789"); |
| 46 | + const bigIntInput = JSONBigUtils.stringify(bigIntObj); |
| 47 | + const bigIntOutput = JSONBigUtils.parse<bigint>(bigIntInput); |
| 48 | + expect(typeof bigIntOutput).toEqual('bigint'); |
| 49 | + |
| 50 | + const idObj: Identifier = { |
| 51 | + version: "version", |
| 52 | + buildTime: "buildTime", |
| 53 | + os: "os", |
| 54 | + osArch: "osArch", |
| 55 | + osVersion: "osVersion", |
| 56 | + cpuCount: "cpuCount", |
| 57 | + maxMemory: "maxMemory", |
| 58 | + launcherName: "launcherName", |
| 59 | + productId: "productId" |
| 60 | + }; |
| 61 | + const idInput = JSONBigUtils.stringify(idObj); |
| 62 | + const idOutput: Identifier = JSONBigUtils.parse<Identifier>(idInput); |
| 63 | + expect(typeof idOutput).toEqual('object'); |
| 64 | + expect(idOutput).toEqual(idObj); |
| 65 | + }); |
| 66 | +}); |
0 commit comments