Skip to content

Commit

Permalink
feat: use CreateInstance
Browse files Browse the repository at this point in the history
CreateInstance
  • Loading branch information
cycjimmy committed Apr 29, 2021
1 parent 9b721a2 commit aa987ad
Show file tree
Hide file tree
Showing 5 changed files with 316 additions and 287 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const wxShare = require('@cycjimmy/weixin-share');
```

```javascript
wxShare
wxShare()
.config([wechatJSSDKConfig])
.setReadyCallBack([wechatConfigReadyCallBack])
.setDefaultShare([defaultShare])
Expand Down Expand Up @@ -74,7 +74,7 @@ wxShare
```html
<script src="weixin-share.umd.min.js"></script>
<script>
wxShare
wxShare()
.config({
appId: [appId],
timestamp: [timestamp],
Expand All @@ -95,7 +95,7 @@ wxShare

To use via a CDN include this in your HTML:
```text
<script src="https://cdn.jsdelivr.net/npm/@cycjimmy/weixin-share@4/dist/weixin-share.umd.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@cycjimmy/weixin-share@5/dist/weixin-share.umd.min.js"></script>
```

<!-- Links: -->
Expand Down
115 changes: 115 additions & 0 deletions __test__/WxShare.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import WxShare from '../src/WxShare';

describe('WxShare', () => {
const wxShare = new WxShare();
const wxConfig = {
appId: 'appId',
nonceStr: "nonceStr",
signature: "signature",
timestamp: "1573456877"
};
const readyCallBack = () => {
console.log('readyCallBack');
};
const shareSuccessCallBack = () => {
console.log('shareSuccessCallBack');
};
const defaultShare = {
title: 'defaultTitle',
link: 'https://default.com',
desc: 'defaultDesc',
imgUrl: 'https://default.com/default.jpg'
};
const customShare = {
title: 'customTitle',
link: 'https://custom.com',
desc: 'customDesc',
imgUrl: 'https://custom.com/custom.jpg'
};

// set config
wxShare.config(wxConfig);

it('wxShare.wxConfig.debug should be false.', () => {
expect(wxShare.wxConfig.debug).toBe(false);
});

it('wxShare.wxConfig.appId should be custom appId.', () => {
expect(wxShare.wxConfig.appId).toBe(wxConfig.appId);
});

it('wxShare.wxConfig.jsApiList should be equal to default jsApiList.', () => {
expect(wxShare.wxConfig.jsApiList).toEqual([
'onMenuShareWeibo',
'updateAppMessageShareData',
'updateTimelineShareData'
]);
});

it('wxShare.readyCallBack should be custom readyCallBack() after wxShare.setReadyCallBack().', () => {
wxShare.setReadyCallBack(readyCallBack);

expect(wxShare.readyCallBack).toBe(readyCallBack);
});

it('wxShare.shareSuccessCallBack should be custom shareSuccessCallBack() after wxShare.setShareSuccessCallBack().', () => {
wxShare.setShareSuccessCallBack(shareSuccessCallBack);

expect(wxShare.shareSuccessCallBack).toBe(shareSuccessCallBack);
});

wxShare.setDefaultShare(defaultShare);

it('wxShare._isInitDefaultShare should be true after wxShare.setDefaultShare().', () => {
expect(wxShare._isInitDefaultShare).toBe(true);
});

it('wxShare.defaultShare.title should be defaultTitle after wxShare.setDefaultShare().', () => {
expect(wxShare.defaultShare.title).toBe(defaultShare.title);
});

it('Mock wxShare._initWxSDK', () => {
// mock _initWxSDK
wxShare._initWxSDK();
wxShare.wx = window.wx = {
config: () => {
},
ready: cb => cb(),
onMenuShareWeibo: () => {
},
updateAppMessageShareData: () => {
},
updateTimelineShareData: () => {
},
};

expect(wxShare.wx).toBeTruthy();
expect(wxShare.wx).toBe(window.wx);
});

it('wxShare.isConfigReady should be true after wxShare._ready().', () => {
return wxShare._ready().then(() => {
expect(wxShare.isConfigReady).toBe(true);
});
});

it('wxShare.share(customShare) should return customShare', () => {
return wxShare.share(customShare)
.then(shareData => {
expect(shareData.title).toBe(customShare.title);
expect(shareData.desc).toBe(customShare.desc);
expect(shareData.link).toBe(customShare.link);
expect(shareData.imgUrl).toBe(customShare.imgUrl);
});
});

it('wxShare.backToDefault() should return defaultShare', () => {
return wxShare.backToDefault()
.then(shareData => {
expect(shareData.title).toBe(defaultShare.title);
expect(shareData.desc).toBe(defaultShare.desc);
expect(shareData.link).toBe(defaultShare.link);
expect(shareData.imgUrl).toBe(defaultShare.imgUrl);
});
});
});
114 changes: 4 additions & 110 deletions __test__/index.test.js
Original file line number Diff line number Diff line change
@@ -1,114 +1,8 @@
import wxShare from '../src/index';

describe('WxShare', () => {
const wxConfig = {
appId: 'appId',
nonceStr: "nonceStr",
signature: "signature",
timestamp: "1573456877"
};
const readyCallBack = () => {
console.log('readyCallBack');
};
const shareSuccessCallBack = () => {
console.log('shareSuccessCallBack');
};
const defaultShare = {
title: 'defaultTitle',
link: 'https://default.com',
desc: 'defaultDesc',
imgUrl: 'https://default.com/default.jpg'
};
const customShare = {
title: 'customTitle',
link: 'https://custom.com',
desc: 'customDesc',
imgUrl: 'https://custom.com/custom.jpg'
};

// set config
wxShare.config(wxConfig);

it('wxShare.wxConfig.debug should be false.', () => {
expect(wxShare.wxConfig.debug).toBe(false);
});

it('wxShare.wxConfig.appId should be custom appId.', () => {
expect(wxShare.wxConfig.appId).toBe(wxConfig.appId);
});

it('wxShare.wxConfig.jsApiList should be equal to default jsApiList.', () => {
expect(wxShare.wxConfig.jsApiList).toEqual([
'onMenuShareWeibo',
'updateAppMessageShareData',
'updateTimelineShareData'
]);
});

it('wxShare.readyCallBack should be custom readyCallBack() after wxShare.setReadyCallBack().', () => {
wxShare.setReadyCallBack(readyCallBack);

expect(wxShare.readyCallBack).toBe(readyCallBack);
});

it('wxShare.shareSuccessCallBack should be custom shareSuccessCallBack() after wxShare.setShareSuccessCallBack().', () => {
wxShare.setShareSuccessCallBack(shareSuccessCallBack);

expect(wxShare.shareSuccessCallBack).toBe(shareSuccessCallBack);
});

wxShare.setDefaultShare(defaultShare);

it('wxShare._isInitDefaultShare should be true after wxShare.setDefaultShare().', () => {
expect(wxShare._isInitDefaultShare).toBe(true);
});

it('wxShare.defaultShare.title should be defaultTitle after wxShare.setDefaultShare().', () => {
expect(wxShare.defaultShare.title).toBe(defaultShare.title);
});

it('Mock wxShare._initWxSDK', () => {
// mock _initWxSDK
wxShare._initWxSDK();
wxShare.wx = window.wx = {
config: () => {
},
ready: cb => cb(),
onMenuShareWeibo: () => {
},
updateAppMessageShareData: () => {
},
updateTimelineShareData: () => {
},
};

expect(wxShare.wx).toBeTruthy();
expect(wxShare.wx).toBe(window.wx);
});

it('wxShare.isConfigReady should be true after wxShare._ready().', () => {
return wxShare._ready().then(() => {
expect(wxShare.isConfigReady).toBe(true);
});
});

it('wxShare.share(customShare) should return customShare', () => {
return wxShare.share(customShare)
.then(shareData => {
expect(shareData.title).toBe(customShare.title);
expect(shareData.desc).toBe(customShare.desc);
expect(shareData.link).toBe(customShare.link);
expect(shareData.imgUrl).toBe(customShare.imgUrl);
});
});

it('wxShare.backToDefault() should return defaultShare', () => {
return wxShare.backToDefault()
.then(shareData => {
expect(shareData.title).toBe(defaultShare.title);
expect(shareData.desc).toBe(defaultShare.desc);
expect(shareData.link).toBe(defaultShare.link);
expect(shareData.imgUrl).toBe(defaultShare.imgUrl);
});
describe('wxShare', () => {
const wxShareIns = wxShare();
it('Singleton mode: wxShareIns should be wxShare().', () => {
expect(wxShareIns).toBe(wxShare());
});
});
Loading

0 comments on commit aa987ad

Please sign in to comment.