Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jesusbotella committed Jun 9, 2020
1 parent e27489e commit fbd9135
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/lib/core/__tests__/Credentials.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Credentials } from '../Credentials';

describe('auth/Credentials', () => {
it('should require a username and an API key', () => {
const creds = new Credentials('aUserName', 'anApiKey');

expect(creds.username).toBe('aUserName');
expect(creds.apiKey).toBe('anApiKey');
});

it('should fail if api key or username are not present', () => {
expect(() => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const creds = new Credentials('aUserName', undefined as any);
expect(creds.username).toBe('aUserName');
}).toThrow();

expect(() => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const creds = new Credentials(undefined as any, 'anApiKey');
expect(creds.username).toBe(undefined);
}).toThrow();
});

it('has a default server', () => {
const creds = new Credentials('aUser', 'anApiKey');
expect(creds.serverURL).toBe('https://aUser.carto.com/');
});

it('can manage different servers', () => {
const customServer = 'http://127.0.0.1:8181/user/{user}';
const creds = new Credentials('aUser', 'anApiKey', customServer);
expect(creds.serverURL).toBe('http://127.0.0.1:8181/user/aUser/');
});
});
29 changes: 29 additions & 0 deletions src/lib/core/__tests__/MetricsEvent.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import MetricsEvent from '../MetricsEvent';

describe('core/MetricsEvent', () => {
it('can be created easily', () => {
const event = new MetricsEvent('mytestlib', 'name of this test');

expect(event.source).toBe('mytestlib');
expect(event.name).toBe('name of this test');
expect(event.groupId).toBeTruthy();
});

it('can be created with a custom id', () => {
const event = new MetricsEvent('mytestlib', 'name of this test', 'id');

expect(event.source).toBe('mytestlib');
expect(event.name).toBe('name of this test');
expect(event.groupId).toBe('id');
});

it('can generate proper headers', () => {
const event = new MetricsEvent('mytestlib', 'name of this test', 'id');

const headers = event.getHeaders();
expect(headers).toBeTruthy();
expect(headers[0]).toEqual(['Carto-Event-Source', 'mytestlib']);
expect(headers[1]).toEqual(['Carto-Event', 'name of this test']);
expect(headers[2]).toEqual(['Carto-Event-Group-Id', 'id']);
});
});

0 comments on commit fbd9135

Please sign in to comment.