Skip to content

Commit

Permalink
feat: added helpers for defer and custom event
Browse files Browse the repository at this point in the history
  • Loading branch information
oxala committed Sep 24, 2018
1 parent 1aac811 commit ea7bf50
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 20 deletions.
24 changes: 17 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { readdirSync } = require('fs');
const {
resolve, join, extname, basename,
resolve: resolvePath, join, extname, basename,
} = require('path');
const { existsSync } = require('fs');
const stackTrace = require('stack-trace');
Expand All @@ -20,12 +20,12 @@ const getFullPath = (componentPath) => {

const index = stack.findIndex((trace) => {
const filename = trace.getFileName();
const fullPath = resolve(filename || '', '..', componentPath);
const fullPath = resolvePath(filename || '', '..', componentPath);

return existsSync(fullPath);
});

return index > -1 && resolve(stack[index].getFileName(), '..', componentPath);
return index > -1 && resolvePath(stack[index].getFileName(), '..', componentPath);
};
const render = (fullPath, withAwait) => (input) => {
/* eslint-disable-next-line global-require, import/no-dynamic-require */
Expand Down Expand Up @@ -71,8 +71,16 @@ const runFixtures = (fixtures, fullPath, withAwait) => (fixtureName) => {

return {};
};

module.exports = (componentPath, { withoutFixtures, withAwait } = {}) => {
const helpers = {
createEvent: eventName => (customEvent => (customEvent.initEvent(eventName, true, true) || customEvent))(document.createEvent('Event')),
defer: () => (toReturn => Object.assign(toReturn, {
promise: new Promise((resolve, reject) => Object.assign(toReturn, {
resolve,
reject,
})),
}))({}),
};
const tester = (componentPath, { withoutFixtures, withAwait } = {}) => {
const fullPath = getFullPath(componentPath);

if (!fullPath) {
Expand All @@ -82,11 +90,13 @@ module.exports = (componentPath, { withoutFixtures, withAwait } = {}) => {
const fixtures = getFixtures();
const boundRunFixtures = runFixtures(fixtures, fullPath, withAwait);

return {
return Object.assign(helpers, {
fixtures: Object.assign(
withoutFixtures ? boundRunFixtures : boundRunFixtures(),
fixtures,
),
render: render(fullPath, withAwait),
};
});
};

module.exports = Object.assign(tester, helpers);
2 changes: 2 additions & 0 deletions tests/components/component/index.marko
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

<button type="button" key="button" on-click('changeInput')>Hello</button>

<button type="button" key="emit-button" on-click('emit', 'hello')/>

<tbody-component attribute=input key="tbody" if(!state.hidden)>
<@body>
body is here
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ Array [
>
Hello
</button>
<button
type="button"
/>
<tbody-component
attribute="{\\"title\\":\\"hello\\",\\"i\\":0,\\"hello\\":\\"world\\"}"
>
Expand All @@ -40,6 +43,9 @@ Array [
>
Hello
</button>
<button
type="button"
/>
</div>,
,
]
Expand All @@ -59,6 +65,9 @@ Array [
>
Hello
</button>
<button
type="button"
/>
<tbody-component
attribute="{\\"hello\\":\\"world\\"}"
>
Expand Down
17 changes: 16 additions & 1 deletion tests/components/component/test/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { render, fixtures } = require('../../../../src/index')('../index.marko', { withoutFixtures: true });
const { render, fixtures, createEvent } = require('../../../../src/index')('../index.marko', { withoutFixtures: true });

fixtures();

Expand Down Expand Up @@ -40,6 +40,21 @@ describe('When component is rendered with data', () => {
expect(component.getEl('tbody')).toBe(undefined);
});
});

describe('When user clicks emit button', () => {
let clickEvent;

beforeEach(() => {
clickEvent = createEvent('click');

jest.spyOn(component, 'emit');
component.getEl('emit-button').dispatchEvent(clickEvent);
});

it('should emit "hello" with event and element', () => {
expect(component.emit).toBeCalledWith('hello', clickEvent, clickEvent.target);
});
});
});

describe('When component is rendered without data', () => {
Expand Down
9 changes: 6 additions & 3 deletions tests/components/non-marko/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
module.exports = (hello) => {
window.alert(hello);
const serviceCall = require('./service');

return '$';
module.exports = {
handledPromise: () => serviceCall.get()
.then(response => Object.assign({ success: true }, response))
.catch(response => Object.assign({ fail: true }, response)),
unhandledPromise: () => serviceCall.get(),
};
1 change: 1 addition & 0 deletions tests/components/non-marko/service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = { get: () => Promise.resolve(true) };
84 changes: 75 additions & 9 deletions tests/components/non-marko/test/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,88 @@
const mockService = { get: jest.fn() };

jest.mock('../service', () => mockService);

const { defer } = require('../../../../src/index');
const util = require('..');

beforeEach(() => {
window.alert = jest.fn();
describe('When handled promise is being called', () => {
let deferred;
let result;

beforeEach(() => {
deferred = defer();

mockService.get.mockReturnValue(deferred.promise);

result = util.handledPromise();
});

describe('When call is successful', () => {
let response;

beforeEach(() => {
response = { hello: 'world' };

deferred.resolve(response);
});

it('should result with success', () => {
expect(result).resolves.toEqual(Object.assign({ success: true }, response));
});
});

describe('When call is failed', () => {
let response;

beforeEach(() => {
response = { world: 'hello' };

deferred.reject(response);
});

it('should result with success', () => {
expect(result).resolves.toEqual(Object.assign({ fail: true }, response));
});
});
});

describe('When util is being called', () => {
const message = 'qwerty';
describe('When unhandled promise is being called', () => {
let deferred;
let result;

beforeEach(() => {
result = util(message);
deferred = defer();

mockService.get.mockReturnValue(deferred.promise);

result = util.unhandledPromise();
});

it('should alert with message', () => {
expect(window.alert).toBeCalledWith(message);
describe('When call is successful', () => {
let response;

beforeEach(() => {
response = { hello: 'world' };

deferred.resolve(response);
});

it('should result with success', () => {
expect(result).resolves.toEqual(response);
});
});

it('should return dollar sign', () => {
expect(result).toBe('$');
describe('When call is failed', () => {
let response;

beforeEach(() => {
response = { world: 'hello' };

deferred.reject(response);
});

it('should result with success', () => {
expect(result).rejects.toEqual(response);
});
});
});

0 comments on commit ea7bf50

Please sign in to comment.