Skip to content

Commit

Permalink
PPI-1030 - Added jest tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cyl3x committed Dec 11, 2024
1 parent 99a713b commit c28ca84
Show file tree
Hide file tree
Showing 11 changed files with 1,527 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Resources/app/administration/jest.setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,9 @@ import 'SwagPayPal/mixin/swag-paypal-credentials-loader.mixin';
import 'SwagPayPal/mixin/swag-paypal-notification.mixin';
import 'SwagPayPal/mixin/swag-paypal-pos-catch-error.mixin';
import 'SwagPayPal/mixin/swag-paypal-pos-log-label.mixin';

import 'SwagPayPal/mixin/swag-paypal-settings.mixin';
import 'SwagPayPal/mixin/swag-paypal-merchant-information.mixin';

import 'SwagPayPal/app/store/swag-paypal-settings.store';
import 'SwagPayPal/app/store/swag-paypal-merchant-information.store';
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { mount } from '@vue/test-utils';
import SwagPayPalOnboardingButton from '.';

Shopware.Component.register('swag-paypal-onboarding-button', Promise.resolve(SwagPayPalOnboardingButton));

async function loadScript(script: Element) {
window.PAYPAL = {
apps: {
// @ts-expect-error - not fully implemented on purpose
Signup: {
setup: jest.fn(),
render: jest.fn(),
},
},
};

script.dispatchEvent(new Event('load'));

await flushPromises();
}

async function createWrapper() {
return mount(
await Shopware.Component.build('swag-paypal-onboarding-button') as typeof SwagPayPalOnboardingButton,
{
global: {
mocks: { $t: (key: string) => key },
provide: {
acl: { can: () => true },
SwagPayPalSettingsService: {
getApiCredentials: jest.fn(),
},
},
},
},
);
}

describe('swag-paypal-onboarding-button', () => {
it('should be a Vue.js component', async () => {
const wrapper = await createWrapper();

expect(wrapper.vm).toBeTruthy();
});

it('should initialize component', async () => {
const wrapper = await createWrapper();

const renderSpy = jest.spyOn(wrapper.vm, 'renderPayPalButton');

const script = document.head.querySelector('#paypal-js');
expect(script).toBeTruthy();
await loadScript(script!);

expect(renderSpy).toHaveBeenCalled();
expect(window.PAYPAL!.apps.Signup.render).toHaveBeenCalled();
expect(window.PAYPAL!.apps.Signup.setup).not.toHaveBeenCalled();
});

it('should set config', async () => {
const wrapper = await createWrapper();
const store = Shopware.Store.get('swagPayPalSettings');

store.setConfig(null, {});
wrapper.setProps({ type: 'live' });
await wrapper.vm.$nextTick();

expect(wrapper.vm.suffix).toBe('');
wrapper.vm.setConfig('client-id', 'client-secret', 'payer-id');

expect(store.allConfigs).toStrictEqual({
null: {
'SwagPayPal.settings.clientId': 'client-id',
'SwagPayPal.settings.clientSecret': 'client-secret',
'SwagPayPal.settings.merchantPayerId': 'payer-id',
'SwagPayPal.settings.sandbox': false,
},
});

store.setConfig(null, {});
wrapper.setProps({ type: 'sandbox' });
await wrapper.vm.$nextTick();

expect(wrapper.vm.suffix).toBe('Sandbox');
wrapper.vm.setConfig('client-id', 'client-secret', 'payer-id');

expect(store.allConfigs).toStrictEqual({
null: {
'SwagPayPal.settings.clientIdSandbox': 'client-id',
'SwagPayPal.settings.clientSecretSandbox': 'client-secret',
'SwagPayPal.settings.merchantPayerIdSandbox': 'payer-id',
'SwagPayPal.settings.sandbox': true,
},
});
});
});
Loading

0 comments on commit c28ca84

Please sign in to comment.