Skip to content

Commit

Permalink
test: 💍 added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ziccardi committed Dec 16, 2020
1 parent f3ab010 commit 0121eb2
Show file tree
Hide file tree
Showing 24 changed files with 673 additions and 431 deletions.
20 changes: 8 additions & 12 deletions test/cmds/app-cmds/create.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {ConsoleMock} from '../../mocks';
import {handler} from '../../../src/cmds/app-cmds/create';
import {initMockEngine} from '../../mocks/UPSMock';
import {getAllApplications, initMockEngine} from '../../mocks/UPSMock';
import {IDGenerator} from '../../mocks/DataStore';

beforeEach(() => {
Expand All @@ -21,20 +21,16 @@ describe('applications', () => {
await handler({
url: 'http://localhost:9999',
name: 'TEST-APP',
output: 'json',
_: [],
$0: '',
});
expect(ConsoleMock.log).toHaveBeenCalledTimes(2);
expect(ConsoleMock.log).toHaveBeenCalledWith(
'Application created successfully'
);
expect(ConsoleMock.log).toHaveBeenCalledWith(
`╔══════════╤══════════════════════════════════════╗
║ NAME │ PUSH-APPLICATION-ID ║
╟──────────┼──────────────────────────────────────╢
║ TEST-APP │ ${appId}
╚══════════╧══════════════════════════════════════╝
`

const testApp = getAllApplications().find(
app => app.pushApplicationID === appId
);

expect(ConsoleMock.log).toHaveBeenCalledTimes(1);
expect(ConsoleMock.log).toHaveBeenCalledWith(JSON.stringify([testApp]));
});
});
11 changes: 3 additions & 8 deletions test/cmds/app-cmds/delete.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import {Arguments} from 'yargs';
import {ConsoleMock} from '../../mocks';
import {handler} from '../../../src/cmds/app-cmds/delete';
import * as inquirer from 'inquirer';
import {
createApplications,
getAllApplications,
Expand All @@ -20,10 +19,6 @@ beforeEach(() => {
ConsoleMock.init();
initMockEngine();
ConsoleMock.mockClear();
const promptMock = (inquirer.prompt as unknown) as jest.Mock<
typeof inquirer.prompt
>;
promptMock.mockClear();
});

afterEach(() => {
Expand All @@ -36,7 +31,7 @@ describe('Delete Application', () => {
// @ts-ignore
await handler({
url: 'http://localhost:9999',
appId: [''],
appId: '',
_: [''],
$0: '',
} as Arguments);
Expand All @@ -49,11 +44,11 @@ describe('Delete Application', () => {
// @ts-ignore
await handler({
url: 'http://localhost:9999',
appId: ['1234'],
name: 'bad name',
_: [''],
$0: '',
} as Arguments);
expect(ConsoleMock.log).toHaveBeenCalled();
expect(ConsoleMock.log).toHaveBeenCalledWith('0 applications deleted');
expect(ConsoleMock.log).toHaveBeenCalledWith('0 application(s) deleted');
});
});
25 changes: 7 additions & 18 deletions test/cmds/app-cmds/list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,23 @@ describe('applications', () => {
const apps = getAllApplications();

// @ts-ignore
await handler({url: 'http://localhost:9999'});
await handler({url: 'http://localhost:9999', output: 'json'});

const res = JSON.parse(ConsoleMock.log.mock.calls[0][0]);
expect(ConsoleMock.log).toHaveBeenCalledTimes(1);
expect(ConsoleMock.log).toHaveBeenCalledWith(
`╔═══════╤══════════════════════════════════════╤══════════╤═══════════════╤═══════════════╗
║ NAME │ PUSH-APPLICATION-ID │ VARIANTS │ INSTALLATIONS │ SENT-MESSAGES ║
╟───────┼──────────────────────────────────────┼──────────┼───────────────┼───────────────╢
${apps[0].name}${apps[0].pushApplicationID} │ 3 │ NaN │ NaN ║
╟───────┼──────────────────────────────────────┼──────────┼───────────────┼───────────────╢
${apps[1].name}${apps[1].pushApplicationID} │ 3 │ NaN │ NaN ║
╟───────┼──────────────────────────────────────┼──────────┼───────────────┼───────────────╢
${apps[2].name}${apps[2].pushApplicationID} │ 3 │ NaN │ NaN ║
╟───────┼──────────────────────────────────────┼──────────┼───────────────┼───────────────╢
${apps[3].name}${apps[3].pushApplicationID} │ 3 │ NaN │ NaN ║
╟───────┼──────────────────────────────────────┼──────────┼───────────────┼───────────────╢
${apps[4].name}${apps[4].pushApplicationID} │ 3 │ NaN │ NaN ║
╚═══════╧══════════════════════════════════════╧══════════╧═══════════════╧═══════════════╝
`
);
expect(res).toMatchObject(apps);
expect(res.length).toEqual(apps.length);
});

it('Should return "no applications found"', async () => {
const filter = {name: 'wrong name'};
// @ts-ignore
await handler({
url: 'http://localhost:9999',
output: 'json',
filter: JSON.stringify(filter),
});
expect(ConsoleMock.log).toHaveBeenCalledTimes(1);
expect(ConsoleMock.log).toHaveBeenCalledWith('No applications found');
expect(ConsoleMock.log).toHaveBeenCalledWith(JSON.stringify([]));
});
});
13 changes: 0 additions & 13 deletions test/cmds/app-cmds/rename.test.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,15 @@
import {ConsoleMock} from '../../mocks';
import {handler} from '../../../src/cmds/app-cmds/rename';
import {UPSAdminClientFactory} from '../../../src/utils/UPSAdminClientFactory';
import * as inquirer from 'inquirer';
import {
createApplications,
getAllApplications,
initMockEngine,
} from '../../mocks/UPSMock';

jest.mock('inquirer', () => ({
prompt: jest
.fn()
.mockReturnValueOnce({confirm: true})
.mockReturnValue({confirm: false}),
}));

beforeEach(() => {
ConsoleMock.init();
initMockEngine();
ConsoleMock.mockClear();
const promptMock = (inquirer.prompt as unknown) as jest.Mock<
typeof inquirer.prompt
>;
promptMock.mockClear();
});

afterEach(() => {
Expand Down
56 changes: 56 additions & 0 deletions test/cmds/variants-cmds/create-cmds/androidVariant.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/* eslint-disable @typescript-eslint/ban-ts-ignore */
import {ConsoleMock} from '../../../mocks';
import {
createApplication,
getAllApplications,
initMockEngine,
} from '../../../mocks/UPSMock';
import {IDGenerator} from '../../../mocks/DataStore';
import {AndroidVariant} from '@aerogear/unifiedpush-admin-client';
import {handler} from '../../../../src/cmds/variants-cmds/create-cmds/andoridVariant';

beforeEach(() => {
// Clear all instances and calls to constructor and all methods:
initMockEngine();
createApplications(10);
ConsoleMock.init();
ConsoleMock.mockClear();
});

afterEach(() => {
ConsoleMock.uninstall();
});

const createApplications = (howmany: number) => {
for (let i = 0; i < howmany; i++) {
createApplication(`TEST APP-${i}`);
}
};

describe('variants create', () => {
it('Should create an android variants', async () => {
const app = getAllApplications()[3];
const variantDef = {
name: 'TEST-ANDROID',
type: 'android',
googleKey: 'TEST-GOOGLE-KEY',
projectNumber: 'TEST-PROJECT-NUMBER',
} as AndroidVariant;

const argv = {
url: 'http://localhost:9999',
appId: app.pushApplicationID,
output: 'json',
...variantDef,
};

const variantID = IDGenerator.peek();

// @ts-ignore
await handler(argv);
expect(ConsoleMock.log).toHaveBeenCalledTimes(1);
expect(JSON.parse(ConsoleMock.log.mock.calls[0][0])).toEqual([
app.variants?.find(v => v.variantID === variantID),
]);
});
});
57 changes: 57 additions & 0 deletions test/cmds/variants-cmds/create-cmds/iosCert.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/* eslint-disable @typescript-eslint/ban-ts-ignore */
import {ConsoleMock} from '../../../mocks';
import {
createApplication,
getAllApplications,
initMockEngine,
} from '../../../mocks/UPSMock';
import {IDGenerator} from '../../../mocks/DataStore';
import {IOSVariantDefinition} from '@aerogear/unifiedpush-admin-client';
import {handler} from '../../../../src/cmds/variants-cmds/create-cmds/iosCert';

beforeEach(() => {
// Clear all instances and calls to constructor and all methods:
initMockEngine();
createApplications(10);
ConsoleMock.init();
ConsoleMock.mockClear();
});

afterEach(() => {
ConsoleMock.uninstall();
});

const createApplications = (howmany: number) => {
for (let i = 0; i < howmany; i++) {
createApplication(`TEST APP-${i}`);
}
};

describe('variants create', () => {
it('Should create an ios variants', async () => {
const app = getAllApplications()[3];
const variantDef = {
name: 'TEST-IOS',
type: 'ios',
certificate: 'test/resources/mockcert.p12',
passphrase: 'PASSWORD',
production: false,
} as IOSVariantDefinition;

const argv = {
url: 'http://localhost:9999',
appId: app.pushApplicationID,
output: 'json',
...variantDef,
};

const variantID = IDGenerator.peek();

// @ts-ignore
await handler(argv);
expect(ConsoleMock.log).toHaveBeenCalledTimes(1);
expect(JSON.parse(ConsoleMock.log.mock.calls[0][0])).toEqual([
app.variants?.find(v => v.variantID === variantID),
]);
});
});
59 changes: 59 additions & 0 deletions test/cmds/variants-cmds/create-cmds/iosToken.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* eslint-disable @typescript-eslint/ban-ts-ignore */
import {ConsoleMock} from '../../../mocks';
import {
createApplication,
getAllApplications,
initMockEngine,
} from '../../../mocks/UPSMock';
import {IDGenerator} from '../../../mocks/DataStore';
import {IOSTokenVariantDefinition} from '@aerogear/unifiedpush-admin-client';
import {handler} from '../../../../src/cmds/variants-cmds/create-cmds/iosToken';

beforeEach(() => {
// Clear all instances and calls to constructor and all methods:
initMockEngine();
createApplications(10);
ConsoleMock.init();
ConsoleMock.mockClear();
});

afterEach(() => {
ConsoleMock.uninstall();
});

const createApplications = (howmany: number) => {
for (let i = 0; i < howmany; i++) {
createApplication(`TEST APP-${i}`);
}
};

describe('variants create', () => {
it('Should create an ios-token variants', async () => {
const app = getAllApplications()[3];
const variantDef = {
name: 'TEST-IOS',
type: 'ios_token',
production: false,
teamId: 'team',
keyId: 'test-key',
bundleId: 'bundle-id',
privateKey: 'test/resources/mock-pkey.pem',
} as IOSTokenVariantDefinition;

const argv = {
url: 'http://localhost:9999',
appId: app.pushApplicationID,
output: 'json',
...variantDef,
};

const variantID = IDGenerator.peek();
// @ts-ignore
await handler(argv);

const newVariant = app.variants?.find(v => v.variantID === variantID)!;
expect(ConsoleMock.log).toHaveBeenCalledTimes(1);
expect(JSON.parse(ConsoleMock.log.mock.calls[0][0])).toEqual([newVariant]);
expect(newVariant.type).toEqual('ios_token');
});
});
57 changes: 57 additions & 0 deletions test/cmds/variants-cmds/create-cmds/webPushVariant.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/* eslint-disable @typescript-eslint/ban-ts-ignore */
import {ConsoleMock} from '../../../mocks';
import {
createApplication,
getAllApplications,
initMockEngine,
} from '../../../mocks/UPSMock';
import {IDGenerator} from '../../../mocks/DataStore';
import {WebPushVariantDefinition} from '@aerogear/unifiedpush-admin-client';
import {handler} from '../../../../src/cmds/variants-cmds/create-cmds/webpushVariant';

beforeEach(() => {
// Clear all instances and calls to constructor and all methods:
initMockEngine();
createApplications(10);
ConsoleMock.init();
ConsoleMock.mockClear();
});

afterEach(() => {
ConsoleMock.uninstall();
});

const createApplications = (howmany: number) => {
for (let i = 0; i < howmany; i++) {
createApplication(`TEST APP-${i}`);
}
};

describe('webpush variants create', () => {
it('Should create an web_push variants', async () => {
const app = getAllApplications()[3];
const variantDef = {
name: 'TEST-WEBPUSH',
type: 'web_push',
publicKey: 'public key',
privateKey: 'private key',
alias: 'mailto:test@aerogear.com',
} as WebPushVariantDefinition;

const argv = {
url: 'http://localhost:9999',
appId: app.pushApplicationID,
output: 'json',
...variantDef,
};

const variantID = IDGenerator.peek();
// @ts-ignore
await handler(argv);

const newVariant = app.variants?.find(v => v.variantID === variantID)!;
expect(ConsoleMock.log).toHaveBeenCalledTimes(1);
expect(JSON.parse(ConsoleMock.log.mock.calls[0][0])).toEqual([newVariant]);
expect(newVariant.type).toEqual('web_push');
});
});
Loading

0 comments on commit 0121eb2

Please sign in to comment.