Skip to content

Commit

Permalink
refactor: working on cookies API (2 part) (#6892)
Browse files Browse the repository at this point in the history
* test: changed tests

* test: added tests Should set on the client and Should delete on the client

* test: fixed test

* test: tuned of a few tests for proxyless
  • Loading branch information
Aleksey28 committed Feb 28, 2022
1 parent eca45c7 commit a18dc6a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
14 changes: 14 additions & 0 deletions test/functional/fixtures/api/es-next/cookies/test.js
@@ -1,3 +1,5 @@
const config = require('../../../../config');

describe('[API] Cookies', function () {
it('Should get cookies by name', function () {
return runTests('./testcafe-fixtures/cookies-test.js', 'Should get cookies by name');
Expand All @@ -15,11 +17,23 @@ describe('[API] Cookies', function () {
return runTests('./testcafe-fixtures/cookies-test.js', 'Should set cookies by key-value');
});

if (!config.proxyless) {
it('Should set on the client', function () {
return runTests('./testcafe-fixtures/cookies-test.js', 'Should set on the client');
});
}

it('Should delete cookies by names and url', function () {
return runTests('./testcafe-fixtures/cookies-test.js', 'Should delete cookies by names and url');
});

it('Should delete cookies by objects', function () {
return runTests('./testcafe-fixtures/cookies-test.js', 'Should delete cookies by objects');
});

if (!config.proxyless) {
it('Should delete on the client', function () {
return runTests('./testcafe-fixtures/cookies-test.js', 'Should delete on the client');
});
}
});
Expand Up @@ -150,14 +150,20 @@ test('Should set cookies by key-value', async t => {
await t.expect(cookies).eql(expectedCookies);
});

test('Should set on the client', async (t) => {
await t.expect(await t.eval(() => document.cookie)).eql('');
await t.setCookies({ name: 'apiCookie13', value: 'value13' });
await t.expect(await t.eval(() => document.cookie)).eql('apiCookie13=value13');
});

fixture`[API] Delete Cookies`
.page('http://localhost:3000/fixtures/api/es-next/cookies/pages/index.html')
.beforeEach(async t => {
await t
.setCookies([
{ name: 'apiCookie1', value: 'value1', domain: 'domain1.com', path: '/' },
{ name: 'apiCookie1', value: 'value1', domain: 'domain2.com', path: '/' },
{ name: 'apiCookie2', value: 'value2', domain: 'domain2.com', path: '/' },
{ name: 'apiCookie1', value: 'value1', domain: 'localhost', path: '/fixtures/api/es-next/cookies/pages/index.html' },
{ name: 'apiCookie2', value: 'value2', domain: 'localhost', path: '/fixtures/api/es-next/cookies/pages/index.html' },
{ name: 'apiCookie3', value: 'value3', domain: 'domain2.com', path: '/path-1' },
{ name: 'apiCookie4', value: 'value4', domain: 'domain1.com', path: '/path-2' },
{ name: 'apiCookie5', value: 'value5', domain: 'domain2.com', path: '/path-1' },
Expand All @@ -169,28 +175,35 @@ fixture`[API] Delete Cookies`

test('Should delete cookies by names and url', async t => {
await t.expect((await t.getCookies()).length).eql(6);
await t.deleteCookies(['apiCookie1', 'apiCookie2'], 'https://domain2.com/');
await t.deleteCookies(['apiCookie1', 'apiCookie2'], 'https://localhost/fixtures/api/es-next/cookies/pages/index.html');

const currentCookies = await t.getCookies();

await t
.expect(currentCookies.length).eql(4)
.expect(currentCookies.some(c => c.name === 'apiCookie1' && c.domain === 'domain2.com')).notOk()
.expect(currentCookies.some(c => c.name === 'apiCookie2' && c.domain === 'domain2.com')).notOk()
.expect(currentCookies.some(c => c.name === 'apiCookie1' && c.domain === 'localhost')).notOk()
.expect(currentCookies.some(c => c.name === 'apiCookie2' && c.domain === 'localhost')).notOk()
.expect(currentCookies.some(c => c.name === 'apiCookie1' && c.domain === 'domain1.com')).ok();
});

test('Should delete cookies by objects', async t => {
await t.expect((await t.getCookies()).length).eql(6);
await t.deleteCookies(
{ name: 'apiCookie1' },
[{ domain: 'domain2.com', path: '/' }],
[{ domain: 'domain2.com', path: '/path-1' }],
);

const currentCookies = await t.getCookies();

await t
.expect(currentCookies.length).eql(3)
.expect(currentCookies.length).eql(2)
.expect(currentCookies.some(c => c.name === 'apiCookie1')).notOk()
.expect(currentCookies.some(c => c.name === 'apiCookie2')).notOk();
.expect(currentCookies.some(c => c.name === 'apiCookie3')).notOk()
.expect(currentCookies.some(c => c.name === 'apiCookie5')).notOk();
});

test('Should delete on the client', async t => {
await t.expect(await t.eval(() => document.cookie)).eql('apiCookie1=value1; apiCookie2=value2');
await t.deleteCookies({ domain: 'localhost', path: '/fixtures/api/es-next/cookies/pages/index.html' });
await t.expect(await t.eval(() => document.cookie)).eql('');
});

0 comments on commit a18dc6a

Please sign in to comment.