-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix issue with generating short url when copying share link (#201475)
## Summary Extracted from #197484, to make it much easier to backport this fix to other releases that require this fix. This PR fixes the issue with generating short URL for the share link. ### Checklist <!-- Check the PR satisfies following conditions. Reviewers should verify this PR satisfies this list as well. - [ ] Any text added follows [EUI's writing guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses sentence case text and includes [i18n support](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md) - [ ] [Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html) was added for features that require explanation or tutorials --> - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios <!-- - [ ] If a plugin configuration key changed, check if it needs to be allowlisted in the cloud and added to the [docker list](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker) - [ ] This was checked for breaking HTTP API changes, and any breaking changes have been approved by the breaking-change committee. The `release_note:breaking` label should be applied in these situations. - [ ] [Flaky Test Runner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was used on any tests changed - [ ] The PR description includes the appropriate Release Notes section, and the correct `release_note:*` label is applied per the [guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process) ### Identify risks Does this PR introduce any risks? For example, consider risks like hard to test bugs, performance regression, potential of data loss. Describe the risk, its severity, and mitigation for each identified risk. Invite stakeholders and evaluate how to proceed before merging. - [ ] [See some risk examples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx) - [ ] ... --> Co-authored-by: Elastic Machine <[email protected]>
- Loading branch information
1 parent
34bf83b
commit 197e606
Showing
6 changed files
with
300 additions
and
78 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
src/plugins/share/public/components/context/index.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
import { renderHook } from '@testing-library/react-hooks'; | ||
import { useShareTabsContext } from '.'; | ||
|
||
describe('share menu context', () => { | ||
describe('useShareTabsContext', () => { | ||
it('throws an error if used outside of ShareMenuProvider tree', () => { | ||
const { result } = renderHook(() => useShareTabsContext()); | ||
|
||
expect(result.error?.message).toEqual( | ||
expect.stringContaining( | ||
'Failed to call `useShareTabsContext` because the context from ShareMenuProvider is missing.' | ||
) | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
191 changes: 191 additions & 0 deletions
191
src/plugins/share/public/components/tabs/link/link_content.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
import React, { type ComponentProps } from 'react'; | ||
import { __IntlProvider as IntlProvider } from '@kbn/i18n-react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import { render, screen, waitFor } from '@testing-library/react'; | ||
|
||
import { urlServiceTestSetup } from '../../../../common/url_service/__tests__/setup'; | ||
import { MockLocatorDefinition } from '../../../../common/url_service/mocks'; | ||
import { BrowserShortUrlClientFactory } from '../../../url_service/short_urls/short_url_client_factory'; | ||
import { | ||
BrowserShortUrlClientHttp, | ||
BrowserShortUrlClient, | ||
} from '../../../url_service/short_urls/short_url_client'; | ||
import { BrowserUrlService } from '../../../types'; | ||
import { LinkContent } from './link_content'; | ||
|
||
const renderComponent = (props: ComponentProps<typeof LinkContent>) => { | ||
render( | ||
<IntlProvider locale="en"> | ||
<LinkContent {...props} /> | ||
</IntlProvider> | ||
); | ||
}; | ||
|
||
describe('LinkContent', () => { | ||
const shareableUrl = 'http://localhost:5601/app/dashboards#/view/123'; | ||
|
||
const http: BrowserShortUrlClientHttp = { | ||
basePath: { | ||
get: () => '/xyz', | ||
}, | ||
fetch: jest.fn(async () => { | ||
return {} as any; | ||
}), | ||
}; | ||
|
||
let urlService: BrowserUrlService; | ||
|
||
// @ts-expect-error there is a type because we override the shortUrls implementation | ||
// eslint-disable-next-line prefer-const | ||
({ service: urlService } = urlServiceTestSetup({ | ||
shortUrls: ({ locators }) => | ||
new BrowserShortUrlClientFactory({ | ||
http, | ||
locators, | ||
}), | ||
})); | ||
|
||
beforeAll(() => { | ||
Object.defineProperty(document, 'execCommand', { | ||
value: jest.fn(() => true), | ||
}); | ||
}); | ||
|
||
it('uses the delegatedShareUrlHandler to generate the shareable URL when it is provided', async () => { | ||
const user = userEvent.setup(); | ||
const objectType = 'dashboard'; | ||
const objectId = '123'; | ||
const isDirty = false; | ||
|
||
const delegatedShareUrlHandler = jest.fn(); | ||
|
||
renderComponent({ | ||
objectType, | ||
objectId, | ||
isDirty, | ||
shareableUrl, | ||
urlService, | ||
allowShortUrl: true, | ||
delegatedShareUrlHandler, | ||
}); | ||
|
||
await user.click(screen.getByTestId('copyShareUrlButton')); | ||
|
||
expect(delegatedShareUrlHandler).toHaveBeenCalled(); | ||
}); | ||
|
||
it('returns the shareable URL when the delegatedShareUrlHandler is not provided and shortURLs are not allowed', async () => { | ||
const user = userEvent.setup(); | ||
const objectType = 'dashboard'; | ||
const objectId = '123'; | ||
const isDirty = false; | ||
|
||
renderComponent({ | ||
objectType, | ||
objectId, | ||
isDirty, | ||
shareableUrl, | ||
urlService, | ||
allowShortUrl: false, | ||
}); | ||
|
||
const copyButton = screen.getByTestId('copyShareUrlButton'); | ||
|
||
await user.click(copyButton); | ||
|
||
waitFor(() => { | ||
expect(copyButton.getAttribute('data-share-url')).toBe(shareableUrl); | ||
}); | ||
}); | ||
|
||
it('invokes the createWithLocator method on the shortURL service if a locator is present when the copy button is clicked', async () => { | ||
const user = userEvent.setup(); | ||
const objectType = 'dashboard'; | ||
const objectId = '123'; | ||
const isDirty = false; | ||
const shareableUrlLocatorParams = { | ||
locator: new MockLocatorDefinition('TEST_LOCATOR'), | ||
params: {}, | ||
}; | ||
|
||
const shortURL = 'http://localhost:5601/xyz/r/s/yellow-orange-tomato'; | ||
|
||
const createWithLocatorSpy = jest.spyOn(BrowserShortUrlClient.prototype, 'createWithLocator'); | ||
|
||
createWithLocatorSpy.mockResolvedValue({ | ||
// @ts-expect-error we only return locator property, as that's all we need for this test | ||
locator: { | ||
getUrl: jest.fn(() => Promise.resolve(shortURL)), | ||
}, | ||
}); | ||
|
||
renderComponent({ | ||
objectType, | ||
objectId, | ||
isDirty, | ||
shareableUrl, | ||
urlService, | ||
allowShortUrl: true, | ||
// @ts-ignore this locator is passed mainly to test the code path that invokes createWithLocator | ||
shareableUrlLocatorParams, | ||
}); | ||
|
||
const copyButton = screen.getByTestId('copyShareUrlButton'); | ||
|
||
const numberOfClicks = 4; | ||
|
||
for (const _click of Array.from({ length: numberOfClicks })) { | ||
await user.click(copyButton); | ||
} | ||
|
||
// should only invoke once no matter how many times the button is clicked | ||
expect(createWithLocatorSpy).toHaveBeenCalledTimes(1); | ||
expect(copyButton.getAttribute('data-share-url')).toBe(shortURL); | ||
}); | ||
|
||
it('invokes the createFromLongUrl method on the shortURL service if a locator is not present when the copy button is clicked', async () => { | ||
const user = userEvent.setup(); | ||
const objectType = 'dashboard'; | ||
const objectId = '123'; | ||
const isDirty = false; | ||
|
||
const shortURL = 'http://localhost:5601/xyz/r/s/yellow-orange-tomato'; | ||
|
||
const createFromLongUrlSpy = jest.spyOn(BrowserShortUrlClient.prototype, 'createFromLongUrl'); | ||
|
||
// @ts-expect-error we only return url property, as that's all we need for this test | ||
createFromLongUrlSpy.mockResolvedValue({ | ||
url: shortURL, | ||
}); | ||
|
||
renderComponent({ | ||
objectType, | ||
objectId, | ||
isDirty, | ||
shareableUrl, | ||
urlService, | ||
allowShortUrl: true, | ||
}); | ||
|
||
const copyButton = screen.getByTestId('copyShareUrlButton'); | ||
|
||
const numberOfClicks = 4; | ||
|
||
for (const _click of Array.from({ length: numberOfClicks })) { | ||
await user.click(copyButton); | ||
} | ||
|
||
// should only invoke once no matter how many times the button is clicked | ||
expect(createFromLongUrlSpy).toHaveBeenCalledTimes(1); | ||
expect(copyButton.getAttribute('data-share-url')).toBe(shortURL); | ||
}); | ||
}); |
Oops, something went wrong.