From 4a81e5584f8483ac21ba845377335c5ca1e05001 Mon Sep 17 00:00:00 2001 From: afc163 Date: Wed, 22 Nov 2017 14:43:30 +0800 Subject: [PATCH] Fix message destroy problem, close #8201 --- components/message/__tests__/index.test.js | 19 +++++++++++++++++++ components/message/index.tsx | 4 ++++ 2 files changed, 23 insertions(+) diff --git a/components/message/__tests__/index.test.js b/components/message/__tests__/index.test.js index b6207435c4..94ae706007 100644 --- a/components/message/__tests__/index.test.js +++ b/components/message/__tests__/index.test.js @@ -88,4 +88,23 @@ describe('message', () => { jest.runAllTimers(); expect(document.querySelectorAll('.ant-message-notice').length).toBe(0); }); + + // https://github.com/ant-design/ant-design/issues/8201 + it('should destroy messages correctly', () => { + // eslint-disable-next-line + class Test extends React.Component { + componentDidMount() { + message.loading('Action in progress1..', 0); + message.loading('Action in progress2..', 0); + setTimeout(() => message.destroy(), 1000); + } + render() { + return
test
; + } + } + mount(); + expect(document.querySelectorAll('.ant-message-notice').length).toBe(2); + jest.runAllTimers(); + expect(document.querySelectorAll('.ant-message-notice').length).toBe(0); + }); }); diff --git a/components/message/index.tsx b/components/message/index.tsx index aa7d5a6730..6b00cf262a 100755 --- a/components/message/index.tsx +++ b/components/message/index.tsx @@ -20,6 +20,10 @@ function getMessageInstance(callback: (i: any) => void) { style: { top: defaultTop }, // 覆盖原来的样式 getContainer, }, (instance: any) => { + if (messageInstance) { + callback(messageInstance); + return; + } messageInstance = instance; callback(instance); });