Skip to content

Commit

Permalink
Fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Fifciu committed Sep 22, 2020
1 parent 78cc3b1 commit 815a088
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 25 deletions.
141 changes: 125 additions & 16 deletions packages/checkout-com/__tests__/configuration.spec.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,64 @@
import { defaultConfig, setup, getPublicKey, getCkoWebhookUrl, getFramesStyles, getFramesLocalization, getCkoProxyUrl, getTransactionTokenKey, getSaveInstrumentKey } from '../src/configuration';
import { defaultConfig, setup, getPublicKey, getCkoWebhookUrl, getFramesStyles, getFramesLocalization, getCkoProxyUrl, getTransactionTokenKey, getSaveInstrumentKey, getCurrentChannel, setChannel } from '../src/configuration';

const consoleLogMock = {
error: jest.fn()
};

Object.defineProperty(window, 'console', {
value: consoleLogMock
});

describe('[checkout-com] configuration', () => {

it('uses fallback values', () => {

const config = {
publicKey: 'some-public-key'
channels: {
en: {
publicKey: '12'
}
},
defaultChannel: 'en'
};

setup(config);

expect(getPublicKey()).toBe(config.publicKey);
expect(getPublicKey()).toBe(config.channels.en.publicKey);
expect(getCkoWebhookUrl()).toBe(defaultConfig.ckoWebHookUrl);
expect(getFramesStyles()).toEqual(defaultConfig.card.style);
expect(getFramesLocalization()).toEqual(defaultConfig.card.localization);
expect(getTransactionTokenKey()).toBe(defaultConfig.tokenizedCardKey);
expect(getCurrentChannel()).toBe(config.defaultChannel);

});

it('appends configuration properly', () => {

const config = {
publicKey: 'some-public-key',
ckoWebHookUrl: 'https://pwebhook.com/api/a',
card: {
style: {ab: '12'},
localization: 'en-US'
channels: {
en: {
publicKey: 'some-public-key',
ckoWebHookUrl: 'https://pwebhook.com/api/a',
card: {
style: {ab: '12'},
localization: 'en-US'
},
tokenizedCardKey: 'temporary-tokenized-value-key',
saveInstrumentKey: 'some-new-value'
}
},
tokenizedCardKey: 'temporary-tokenized-value-key',
saveInstrumentKey: 'some-new-value'
defaultChannel: 'en'
};

setup(config);

expect(getPublicKey()).toBe(config.publicKey);
expect(getCkoWebhookUrl()).toBe(config.ckoWebHookUrl);
expect(getFramesStyles()).toEqual(config.card.style);
expect(getFramesLocalization()).toEqual(config.card.localization);
expect(getTransactionTokenKey()).toBe(config.tokenizedCardKey);
expect(getSaveInstrumentKey()).toBe(config.saveInstrumentKey);
expect(getPublicKey()).toBe(config.channels.en.publicKey);
expect(getCkoWebhookUrl()).toBe(config.channels.en.ckoWebHookUrl);
expect(getFramesStyles()).toEqual(config.channels.en.card.style);
expect(getFramesLocalization()).toEqual(config.channels.en.card.localization);
expect(getTransactionTokenKey()).toBe(config.channels.en.tokenizedCardKey);
expect(getSaveInstrumentKey()).toBe(config.channels.en.saveInstrumentKey);
expect(getCurrentChannel()).toBe(config.defaultChannel);

});

Expand All @@ -48,4 +68,93 @@ describe('[checkout-com] configuration', () => {

});

it('set ups new channel properly', () => {

const config = {
channels: {
en: {
publicKey: 'some-public-key',
ckoWebHookUrl: 'https://pwebhook.com/api/a',
card: {
style: {ab: '12'},
localization: 'en-US'
},
tokenizedCardKey: 'temporary-tokenized-value-key',
saveInstrumentKey: 'some-new-value'
},
it: {
publicKey: 'some-public-xcxcxc-asdas',
ckoWebHookUrl: 'https://abcc.com/api/bbba',
card: {
style: {asdas: '1552'},
localization: 'it-IT'
},
tokenizedCardKey: 'some-token-value-it',
saveInstrumentKey: 'some-new-value-it-ab'
}
},
defaultChannel: 'en'
};

setup(config);
const newChannel = 'it';
setChannel(newChannel);

expect(getPublicKey()).toBe(config.channels.it.publicKey);
expect(getCkoWebhookUrl()).toBe(config.channels.it.ckoWebHookUrl);
expect(getFramesStyles()).toEqual(config.channels.it.card.style);
expect(getFramesLocalization()).toEqual(config.channels.it.card.localization);
expect(getTransactionTokenKey()).toBe(config.channels.it.tokenizedCardKey);
expect(getSaveInstrumentKey()).toBe(config.channels.it.saveInstrumentKey);
expect(getCurrentChannel()).toBe(newChannel);

});

it('prints error if not existing channel is default', () => {

const config = {
channels: {
en: {
publicKey: 'some-public-key',
ckoWebHookUrl: 'https://pwebhook.com/api/a',
card: {
style: {ab: '12'},
localization: 'en-US'
},
tokenizedCardKey: 'temporary-tokenized-value-key',
saveInstrumentKey: 'some-new-value'
}
},
defaultChannel: 'ch'
};

setup(config);
expect(consoleLogMock.error).toHaveBeenCalledWith('[CKO] Bad config provided');

});

it('prints error if not existing channel is default', () => {

const config = {
channels: {
en: {
publicKey: 'some-public-key',
ckoWebHookUrl: 'https://pwebhook.com/api/a',
card: {
style: {ab: '12'},
localization: 'en-US'
},
tokenizedCardKey: 'temporary-tokenized-value-key',
saveInstrumentKey: 'some-new-value'
}
},
defaultChannel: 'en'
};

setup(config);
setChannel('it');
expect(consoleLogMock.error).toHaveBeenCalledWith('[CKO] Requested channel does not exist in the config');

});

});
14 changes: 12 additions & 2 deletions packages/checkout-com/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { setup, getPublicKey, getCkoWebhookUrl, getFramesStyles, useCko, CkoPaymentType } from '../src/index';
import { setup, getPublicKey, getCkoWebhookUrl, getFramesStyles, useCko, setChannel, CkoPaymentType } from '../src/index';

jest.mock('../src/configuration.ts', () => ({
setup: jest.fn(),
getPublicKey: jest.fn(),
getCkoWebhookUrl: jest.fn(),
setChannel: jest.fn(),
getFramesStyles: jest.fn()
}));

Expand All @@ -13,18 +14,27 @@ describe('index.ts', () => {

it('exports proper functions', () => {
setup({
publicKey: '12'
channels: {
en: {
publicKey: '12'
}
},
defaultChannel: 'en'
});
getPublicKey();
getCkoWebhookUrl();
getFramesStyles();
useCko();

const newChannel = 'it';
setChannel(newChannel);

expect(setup).toHaveBeenCalled();
expect(getPublicKey).toHaveBeenCalled();
expect(getCkoWebhookUrl).toHaveBeenCalled();
expect(getFramesStyles).toHaveBeenCalled();
expect(useCko).toHaveBeenCalled();
expect(setChannel).toHaveBeenCalledWith(newChannel);
});

it('exports proper CkoPaymentType enum', () => {
Expand Down
18 changes: 11 additions & 7 deletions packages/checkout-com/__tests__/payment.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { createContext, createPayment, getCustomerCards, removeSavedCard } from
const publicKey = 'public key';
const ckoWebhookUrl = 'https://webhook.com/api';
const ckoProxyUrl = 'https://proxy.com/api';
const currentChannel = 'en';

import axios from 'axios';
jest.mock('axios', () => ({
Expand All @@ -12,7 +13,8 @@ jest.mock('axios', () => ({
jest.mock('@vue-storefront/checkout-com/src/configuration', () => ({
getPublicKey: () => publicKey,
getCkoWebhookUrl: () => ckoWebhookUrl,
getCkoProxyUrl: () => ckoProxyUrl
getCkoProxyUrl: () => ckoProxyUrl,
getCurrentChannel: () => currentChannel
}));

describe('[checkout-com] payment', () => {
Expand Down Expand Up @@ -113,16 +115,17 @@ describe('[checkout-com] payment', () => {
it('getCustomerCards', () => {

/*eslint-disable */
const customer = {
customer_id: 15
const payload = {
customer_id: 15,
channel: currentChannel
};
/* eslint-enable */

getCustomerCards(customer);
getCustomerCards(payload);

expect(axios.post).toBeCalledWith(
`${ckoProxyUrl}/payment-instruments`,
customer
payload
);

});
Expand All @@ -132,14 +135,15 @@ describe('[checkout-com] payment', () => {
/*eslint-disable */
const customerData = {
customer_id: 15,
payment_instrument_id: 12
payment_instrument_id: 12,
channel: currentChannel
};
/* eslint-enable */

removeSavedCard(customerData);

expect(axios.delete).toBeCalledWith(
`${ckoProxyUrl}/payment-instruments/${customerData.customer_id}/${customerData.payment_instrument_id}`
`${ckoProxyUrl}/payment-instruments/${customerData.customer_id}/${customerData.payment_instrument_id}/${customerData.channel}`
);

});
Expand Down

0 comments on commit 815a088

Please sign in to comment.