|
| 1 | +import { Nightfall } from '../nightfall' |
| 2 | +import { creditCardConfig, creditCardPayload, errorResponse } from './mocks' |
| 3 | + |
| 4 | +describe('should test the text scanning method', () => { |
| 5 | + // Set API key and other dependencies |
| 6 | + if (!process.env.NIGHTFALL_API_KEY) { |
| 7 | + throw new Error("NIGHTFALL_API_KEY environment variable is required") |
| 8 | + } |
| 9 | + |
| 10 | + const apiKey = process.env.NIGHTFALL_API_KEY |
| 11 | + |
| 12 | + // Run tests |
| 13 | + it('should create a new nightfall client and check if the scanText method exists', () => { |
| 14 | + const client = new Nightfall(apiKey) |
| 15 | + |
| 16 | + expect(client).toBeDefined() |
| 17 | + expect(typeof client.scanText).toBe('function') |
| 18 | + }) |
| 19 | + |
| 20 | + it('should return an error if the request was configured incorrectly', async () => { |
| 21 | + const client = new Nightfall(apiKey) |
| 22 | + const scanTextSpy = jest.spyOn(client, 'scanText') |
| 23 | + |
| 24 | + const response = await client.scanText(creditCardPayload, {}) |
| 25 | + expect(scanTextSpy).toHaveBeenCalledWith(creditCardPayload, {}) |
| 26 | + expect(response.data).toBeUndefined() |
| 27 | + expect(response.isError).toBe(true) |
| 28 | + const error = response.getError() |
| 29 | + expect(error).toEqual(errorResponse) |
| 30 | + }) |
| 31 | + |
| 32 | + it('should return findings', async () => { |
| 33 | + const client = new Nightfall(apiKey) |
| 34 | + const scanTextSpy = jest.spyOn(client, 'scanText') |
| 35 | + |
| 36 | + const response = await client.scanText(creditCardPayload, creditCardConfig) |
| 37 | + expect(scanTextSpy).toHaveBeenCalledWith(creditCardPayload, creditCardConfig) |
| 38 | + expect(response.isError).toBe(false) |
| 39 | + expect(response.data).toBeDefined() |
| 40 | + expect(response.data).toHaveProperty('findings') |
| 41 | + expect(response.data?.findings).toHaveLength(1) |
| 42 | + }) |
| 43 | +}) |
0 commit comments