Open jpzwarte opened 6 years ago
@jpzwarte
Can you please share your code that sets the state? I'm using ng-apimock for more than 2 years now and don't have problems with it. My first guess would be a promise implementation on your side.
@wswebcreation see https://gist.github.com/jpzwarte/a98149513b0f7437d3957d07abe79da8
@wswebcreation @mdasberg the hack below works 💪
// HACK: intercept the uuid call that ng-apimock makes in order to
// get the ngapimockid. There's a bug where the cookie is set too late,
// so fix that by doing it ourselves.
const uuid = require('../node_modules/ng-apimock/node_modules/uuid'),
originalFn = uuid.v4;
let ngapimockId;
uuid.v4 = function () {
const result = ngapimockId = originalFn();
return result;
}
// We need to be on a page in order to set the cookies
browser.driver.get(browser.baseUrl);
// Load ngApimock
global['ngApimock'] = require('../.tmp/ngApimock/protractor.mock.js');
beforeEach(() => global['ngApimock'].setAllScenariosToDefault());
// Manually set ngapimockid cookie
browser.manage().addCookie({ name: 'ngapimockid', value: ngapimockId });
@jpzwarte
I never relied on the control-flow of Protractor, so I don't know if
fit('should show a placeholder when no groups exist', () => {
ngApimock.selectScenario('teacher-groups', 'empty');
page.navigateTo();
page.sleep(60000);
expect(page.chooseExistingGroups.animation).toBeDisplayed();
expect(page.chooseExistingGroups.pointers).toBeDisplayed();
});
Works.
In my setup I'd always use this
fit('should show a placeholder when no groups exist', async () => {
await ngApimock.selectScenario('teacher-groups', 'empty');
await page.navigateTo();
expect(await page.chooseExistingGroups.animation).toBeDisplayed();
expect(await page.chooseExistingGroups.pointers).toBeDisplayed();
});
And this never gives problems
I thought about this, for example i tried adding async/wait for the selectScenario
call and a browser.sleep()
before the page.navigateTo()
but that didn't work. Anyway, i heard via some people that this issue may be fixed in another branch and may be backported. In the mean time i'll use my hack :)
👍
Can we close the issue then?
The bug report is still valid right? If so, i would keep it open to track progress.
The problem i'm seeing is that the ngapimockid cookie is set too late, causing the calls to the (mock) backend to lack the cookie and always returning the default mocks, eventhough i'm calling
selectScenario
. See my analysis below.When i add some
console.log
calls to myonPrepare
method inprotractor.conf
and to theng-apimock/lib/utils
ngApimockRequest
function, i see the following behavior:PUT /ngapimock/mocks/defaults c3b27ce7-503a-4069-900c-60087b52471e
This is the
setAllScenariosToDefault()
call. The uuid is the ngapimockid value.I then see the front-end doing a call:
GET /teacher/groups undefined
. Theundefined
here is the ngapimockid value.After that, i get the
console.log
i put in the "hooker" code in.tmp/ngApimock/protractor.mock.js
:When i add a
browser.sleep(60000);
in myit()
and then refresh the browser, the ngapimockid cookie is present and passed to the mock server. I then get the appropriate mock back.