diff --git a/platform/core/src/views/ViewProvider.js b/platform/core/src/views/ViewProvider.js index d1535328e8..50b47f4068 100644 --- a/platform/core/src/views/ViewProvider.js +++ b/platform/core/src/views/ViewProvider.js @@ -84,7 +84,7 @@ define( // Check if a view and domain object type can be paired; // both can restrict the others they accept. function viewMatchesType(view, type) { - var views = type && type.getDefinition().views, + var views = type && (type.getDefinition() || {}).views, matches = true; // View is restricted to a certain type diff --git a/platform/core/test/views/ViewProviderSpec.js b/platform/core/test/views/ViewProviderSpec.js index 47d44d0058..bb0fdcf552 100644 --- a/platform/core/test/views/ViewProviderSpec.js +++ b/platform/core/test/views/ViewProviderSpec.js @@ -87,6 +87,57 @@ define( expect(mockLog.warn).toHaveBeenCalledWith(jasmine.any(String)); }); + it("restricts typed views to matching types", function () { + var testType = "testType", + testView = { key: "x", type: testType }, + provider = new ViewProvider([testView], mockLog); + + // Include a "type" capability + capabilities.type = jasmine.createSpyObj( + "type", + ["instanceOf", "invoke", "getDefinition"] + ); + capabilities.type.invoke.andReturn(capabilities.type); + + // Should be included when types match + capabilities.type.instanceOf.andReturn(true); + expect(provider.getViews(mockDomainObject)) + .toEqual([testView]); + expect(capabilities.type.instanceOf) + .toHaveBeenCalledWith(testType); + + // ...but not when they don't + capabilities.type.instanceOf.andReturn(false); + expect(provider.getViews(mockDomainObject)) + .toEqual([]); + + }); + + it("enforces view restrictions from types", function () { + var testType = "testType", + testView = { key: "x" }, + provider = new ViewProvider([testView], mockLog); + + // Include a "type" capability + capabilities.type = jasmine.createSpyObj( + "type", + ["instanceOf", "invoke", "getDefinition"] + ); + capabilities.type.invoke.andReturn(capabilities.type); + + // Should be included when view keys match + capabilities.type.getDefinition + .andReturn({ views: [testView.key]}); + expect(provider.getViews(mockDomainObject)) + .toEqual([testView]); + + // ...but not when they don't + capabilities.type.getDefinition + .andReturn({ views: ["somethingElse"]}); + expect(provider.getViews(mockDomainObject)) + .toEqual([]); + }); + }); } ); \ No newline at end of file