Skip to content

Commit

Permalink
Merge pull request #118 from opencomponents/remove-rendered-logic
Browse files Browse the repository at this point in the history
remove logic of api returning a "rendered" response as it can't happen
  • Loading branch information
ricardo-devis-agullo authored Aug 29, 2024
2 parents 267858a + 01b0077 commit 3637eb4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 118 deletions.
63 changes: 17 additions & 46 deletions src/oc-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ var oc = oc || {};
MESSAGES_ERRORS_RETRY_FAILED =
'Failed to load $0 component ' + RETRY_LIMIT + ' times. Giving up',
MESSAGES_ERRORS_LOADING_COMPILED_VIEW = 'Error getting compiled view: $0',
MESSAGES_ERRORS_GETTING_DATA = 'Error getting data',
MESSAGES_ERRORS_RENDERING = 'Error rendering component: $1, error: $0',
MESSAGES_ERRORS_RETRIEVING =
'Failed to retrieve the component. Retrying in ' +
Expand Down Expand Up @@ -277,9 +276,6 @@ var oc = oc || {};
crossDomain: true,
success: function (apiResponse) {
var response = apiResponse[0].response;
if (response.renderMode == 'rendered') {
return cb(MESSAGES_ERRORS_GETTING_DATA);
}
var err = response.error ? response.details || response.error : null;
cb(err, response.data, apiResponse[0]);
},
Expand Down Expand Up @@ -530,49 +526,24 @@ var oc = oc || {};
crossDomain: true,
success: function (apiResponse) {
var template = apiResponse.template;
if (apiResponse.renderMode == 'unrendered') {
apiResponse.data.id = id;
oc.render(template, apiResponse.data, function (err, html) {
if (err) {
callback(
interpolate(
MESSAGES_ERRORS_RENDERING,
err,
apiResponse.href
)
);
} else {
logInfo(interpolate(MESSAGES_RENDERED, template.src));
callback(null, {
id: id,
html: html,
baseUrl: apiResponse.baseUrl,
key: template.key,
version: apiResponse.version,
name: apiResponse.name
});
}
});
} else if (apiResponse.renderMode == 'rendered') {
logInfo(interpolate(MESSAGES_RENDERED, apiResponse.href));

if (apiResponse.html.indexOf('<' + OC_TAG) == 0) {
var innerHtmlPlusEnding = apiResponse.html.slice(
apiResponse.html.indexOf('>') + 1
),
innerHtml = innerHtmlPlusEnding.slice(
0,
innerHtmlPlusEnding.lastIndexOf('<')
);

apiResponse.html = innerHtml;
apiResponse.data.id = id;
oc.render(template, apiResponse.data, function (err, html) {
if (err) {
callback(
interpolate(MESSAGES_ERRORS_RENDERING, err, apiResponse.href)
);
} else {
logInfo(interpolate(MESSAGES_RENDERED, template.src));
callback(null, {
id: id,
html: html,
baseUrl: apiResponse.baseUrl,
key: template.key,
version: apiResponse.version,
name: apiResponse.name
});
}
callback(null, {
html: apiResponse.html,
version: apiResponse.version,
name: apiResponse.name
});
}
});
},
error: function (err) {
if (err && err.status == 429) {
Expand Down
24 changes: 0 additions & 24 deletions test/get-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,30 +148,6 @@ describe('oc-client : getData', function () {
);
});

it('should call the callback with an error if the registry responds with a rendered component', function (done) {
oc.$.ajax = function (options) {
return options.success([
{ response: { renderMode: 'rendered', data: 'hello' } }
]);
};

execute(
{
baseUrl: 'http://www.components.com/v2',
name: 'myComponent',
version: '6.6.6',
parameters: {
name: 'evil'
}
},
function (err, data) {
expect(err).toEqual('Error getting data');
expect(data).toEqual(undefined);
done();
}
);
});

describe('when json is requested', function () {
it('should call the $.ajax method correctly', function (done) {
var spy = sinon.spy(oc.$, 'ajax');
Expand Down
48 changes: 0 additions & 48 deletions test/render-by-href.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,54 +158,6 @@ describe('oc-client : renderByHref', function () {
});
});

describe('when the registry responds with rendered component with container', function () {
var callback;
beforeEach(function () {
callback = sinon.spy();
initialise(renderedResponse);
eval(compiledViewContent);
oc.renderByHref(route, callback);
});

afterEach(cleanup);

it('should respond without an error', function () {
expect(callback.args[0][0]).toBe(null);
});

it('should respond with the rendered html', function () {
expect(callback.args[0][1].html).toEqual('Hello, world!!!');
});

it('should respond with the correct version', function () {
expect(callback.args[0][1].version).toEqual('1.2.123');
});
});

describe('when the registry responds with rendered component without container', function () {
var callback;
beforeEach(function () {
callback = sinon.spy();
initialise(renderedNoContainerResponse);
eval(compiledViewContent);
oc.renderByHref(route, callback);
});

afterEach(cleanup);

it('should respond without an error', function () {
expect(callback.args[0][0]).toBe(null);
});

it('should respond with the rendered html', function () {
expect(callback.args[0][1].html).toEqual('Hello, world!!');
});

it('should respond with the correct version', function () {
expect(callback.args[0][1].version).toEqual('1.2.123');
});
});

describe('when getting component returns an error', function () {
var ajaxMock, error;
beforeEach(function (done) {
Expand Down

0 comments on commit 3637eb4

Please sign in to comment.