|
| 1 | +import * as core from '../src/core'; |
| 2 | +import { Report } from '../src/report'; |
1 | 3 | import * as Wpmp from 'window-post-message-proxy'; |
2 | 4 | import * as Hpm from 'http-post-message'; |
3 | 5 | import * as Router from 'powerbi-router'; |
4 | 6 | import { spyApp, setup } from './utility/mockReportEmbed'; |
| 7 | +import * as factories from '../src/factories'; |
| 8 | +import { spyHpm } from './utility/mockHpm'; |
5 | 9 |
|
6 | 10 | declare global { |
7 | 11 | interface Window { |
@@ -1787,3 +1791,154 @@ describe('Protocol', function () { |
1787 | 1791 | }); |
1788 | 1792 | }); |
1789 | 1793 | }); |
| 1794 | + |
| 1795 | +describe('SDK-to-MockApp (UNIT tests)', function () { |
| 1796 | + let $element: JQuery; |
| 1797 | + let iframe: HTMLIFrameElement; |
| 1798 | + let iframeHpm: Hpm.HttpPostMessage; |
| 1799 | + let powerbi: core.PowerBi; |
| 1800 | + let report: Report; |
| 1801 | + let hpmSpy: jasmine.Spy; |
| 1802 | + |
| 1803 | + beforeAll(function () { |
| 1804 | + const spyHpmFactory: factories.IHpmFactory = () => { |
| 1805 | + return <Hpm.HttpPostMessage><any>spyHpm; |
| 1806 | + }; |
| 1807 | + const noop: factories.IWpmpFactory = () => { |
| 1808 | + return <Wpmp.WindowPostMessageProxy>null; |
| 1809 | + }; |
| 1810 | + |
| 1811 | + powerbi = new core.PowerBi(spyHpmFactory, noop); |
| 1812 | + |
| 1813 | + $element = $(`<div class="powerbi-report-container"></div>`) |
| 1814 | + .appendTo(document.body); |
| 1815 | + |
| 1816 | + const iframeSrc = "base/e2e/utility/noop.html"; |
| 1817 | + const embedConfiguration = { |
| 1818 | + type: "report", |
| 1819 | + reportId: "fakeReportId", |
| 1820 | + accessToken: 'fakeToken', |
| 1821 | + embedUrl: iframeSrc |
| 1822 | + }; |
| 1823 | + report = <Report>powerbi.embed($element[0], embedConfiguration); |
| 1824 | + |
| 1825 | + iframe = <HTMLIFrameElement>$element.find('iframe')[0]; |
| 1826 | + |
| 1827 | + // Register Iframe side |
| 1828 | + iframeHpm = setup(iframe.contentWindow, window, true); |
| 1829 | + |
| 1830 | + // Reset load handler |
| 1831 | + spyHpm.post.calls.reset(); |
| 1832 | + }); |
| 1833 | + |
| 1834 | + afterAll(function () { |
| 1835 | + // TODO: Should call remove using the powerbi service first to clean up intenral references to DOM inside this element |
| 1836 | + $element.remove(); |
| 1837 | + }); |
| 1838 | + |
| 1839 | + describe('SDK-to-HPM', function () { |
| 1840 | + afterEach(function () { |
| 1841 | + spyHpm.get.calls.reset(); |
| 1842 | + spyHpm.post.calls.reset(); |
| 1843 | + spyHpm.patch.calls.reset(); |
| 1844 | + spyHpm.put.calls.reset(); |
| 1845 | + spyHpm.delete.calls.reset(); |
| 1846 | + }); |
| 1847 | + |
| 1848 | + describe('load', function () { |
| 1849 | + it('report.load() sends POST /report/load with configuration in body', function () { |
| 1850 | + // Arrange |
| 1851 | + const testData = { |
| 1852 | + embedConfiguration: { |
| 1853 | + id: 'fakeId', |
| 1854 | + accessToken: 'fakeToken' |
| 1855 | + } |
| 1856 | + }; |
| 1857 | + |
| 1858 | + spyHpm.post.and.returnValue(Promise.resolve(null)); |
| 1859 | + |
| 1860 | + // Act |
| 1861 | + report.load(testData.embedConfiguration); |
| 1862 | + |
| 1863 | + // Assert |
| 1864 | + expect(spyHpm.post).toHaveBeenCalledWith('/report/load', testData.embedConfiguration); |
| 1865 | + }); |
| 1866 | + |
| 1867 | + it('report.load() returns promise that rejects with validation error if the load configuration is invalid', function (done) { |
| 1868 | + // Arrange |
| 1869 | + const testData = { |
| 1870 | + embedConfiguration: { |
| 1871 | + id: 'fakeId', |
| 1872 | + accessToken: 'fakeToken' |
| 1873 | + }, |
| 1874 | + errorResponse: { |
| 1875 | + body: { |
| 1876 | + message: "invalid configuration object" |
| 1877 | + } |
| 1878 | + } |
| 1879 | + }; |
| 1880 | + |
| 1881 | + spyHpm.post.and.returnValue(Promise.reject(testData.errorResponse)); |
| 1882 | + |
| 1883 | + // Act |
| 1884 | + report.load(testData.embedConfiguration) |
| 1885 | + .catch(error => { |
| 1886 | + expect(spyHpm.post).toHaveBeenCalledWith('/report/load', testData.embedConfiguration); |
| 1887 | + expect(error).toEqual(testData.errorResponse.body); |
| 1888 | + // Assert |
| 1889 | + done(); |
| 1890 | + }); |
| 1891 | + }); |
| 1892 | + |
| 1893 | + it('report.load() returns promise that rejects with server error if there was an error loading the report', function (done) { |
| 1894 | + // Arrange |
| 1895 | + const testData = { |
| 1896 | + embedConfiguration: { |
| 1897 | + id: 'fakeId', |
| 1898 | + accessToken: 'fakeToken' |
| 1899 | + }, |
| 1900 | + errorResponse: { |
| 1901 | + body: { |
| 1902 | + message: "Access Token is invalid" |
| 1903 | + } |
| 1904 | + } |
| 1905 | + }; |
| 1906 | + |
| 1907 | + spyHpm.post.and.returnValue(Promise.reject(testData.errorResponse)); |
| 1908 | + |
| 1909 | + // Act |
| 1910 | + report.load(testData.embedConfiguration) |
| 1911 | + .catch(error => { |
| 1912 | + expect(spyHpm.post).toHaveBeenCalledWith('/report/load', testData.embedConfiguration); |
| 1913 | + expect(error).toEqual(testData.errorResponse.body); |
| 1914 | + // Assert |
| 1915 | + done(); |
| 1916 | + }); |
| 1917 | + }); |
| 1918 | + }); |
| 1919 | + |
| 1920 | + describe('pages', function () { |
| 1921 | + it('report.getPages() sends GET /report/pages', function () { |
| 1922 | + // Arrange |
| 1923 | + const testData = { |
| 1924 | + embedConfiguration: { |
| 1925 | + id: 'fakeId', |
| 1926 | + accessToken: 'fakeToken' |
| 1927 | + } |
| 1928 | + }; |
| 1929 | + |
| 1930 | + // Act |
| 1931 | + report.load(testData.embedConfiguration); |
| 1932 | + |
| 1933 | + // Assert |
| 1934 | + expect(spyHpm.post).toHaveBeenCalledWith('/report/load', testData.embedConfiguration); |
| 1935 | + }); |
| 1936 | + }); |
| 1937 | + }); |
| 1938 | + |
| 1939 | + // describe('REPORT-to-SDK', function () { |
| 1940 | + // it('should fail', function () { |
| 1941 | + // expect(true).toBe(false); |
| 1942 | + // }); |
| 1943 | + // }); |
| 1944 | +}); |
0 commit comments