|
9 | 9 |
|
10 | 10 | jest.dontMock('../CoreManager');
|
11 | 11 | jest.dontMock('../encode');
|
| 12 | +jest.dontMock('../decode'); |
12 | 13 | jest.dontMock('../ParseError');
|
13 | 14 | jest.dontMock('../ParseGeoPoint');
|
14 | 15 | jest.dontMock('../ParsePromise');
|
15 | 16 | jest.dontMock('../ParseQuery');
|
| 17 | +jest.dontMock('../SingleInstanceStateController'); |
| 18 | +jest.dontMock('../UniqueInstanceStateController'); |
| 19 | +jest.dontMock('../ObjectStateMutations'); |
16 | 20 |
|
17 | 21 | var mockObject = function(className) {
|
18 | 22 | this.className = className;
|
@@ -1332,4 +1336,223 @@ describe('ParseQuery', () => {
|
1332 | 1336 | done();
|
1333 | 1337 | });
|
1334 | 1338 | });
|
| 1339 | + |
| 1340 | + |
| 1341 | + |
| 1342 | + it('overrides cached object with query results', (done) => { |
| 1343 | + jest.dontMock("../ParseObject"); |
| 1344 | + jest.resetModules(); |
| 1345 | + ParseObject = require('../ParseObject').default; |
| 1346 | + CoreManager = require('../CoreManager'); |
| 1347 | + ParseQuery = require('../ParseQuery').default; |
| 1348 | + |
| 1349 | + ParseObject.enableSingleInstance(); |
| 1350 | + |
| 1351 | + var objectToReturn = { |
| 1352 | + objectId: 'T01', |
| 1353 | + name: 'Name', |
| 1354 | + other: 'other', |
| 1355 | + className:"Thing", |
| 1356 | + createdAt: '2017-01-10T10:00:00Z' |
| 1357 | + }; |
| 1358 | + |
| 1359 | + CoreManager.setQueryController({ |
| 1360 | + find(className, params, options) { |
| 1361 | + return ParsePromise.as({ |
| 1362 | + results: [objectToReturn] |
| 1363 | + }); |
| 1364 | + } |
| 1365 | + }); |
| 1366 | + |
| 1367 | + var q = new ParseQuery("Thing"); |
| 1368 | + var testObject; |
| 1369 | + q.find().then((results) => { |
| 1370 | + testObject = results[0]; |
| 1371 | + |
| 1372 | + expect(testObject.get("name")).toBe("Name"); |
| 1373 | + expect(testObject.get("other")).toBe("other"); |
| 1374 | + |
| 1375 | + objectToReturn = { objectId: 'T01', name: 'Name2'}; |
| 1376 | + var q2 = new ParseQuery("Thing"); |
| 1377 | + return q2.find(); |
| 1378 | + }).then((results) => { |
| 1379 | + expect(results[0].get("name")).toBe("Name2"); |
| 1380 | + expect(results[0].has("other")).toBe(false); |
| 1381 | + }).then(() => { |
| 1382 | + expect(testObject.get("name")).toBe("Name2"); |
| 1383 | + expect(testObject.has("other")).toBe(false); |
| 1384 | + done(); |
| 1385 | + }); |
| 1386 | + }); |
| 1387 | + |
| 1388 | + it('does not override unselected fields with select query results', (done) => { |
| 1389 | + jest.dontMock("../ParseObject"); |
| 1390 | + jest.resetModules(); |
| 1391 | + ParseObject = require('../ParseObject').default; |
| 1392 | + CoreManager = require('../CoreManager'); |
| 1393 | + ParseQuery = require('../ParseQuery').default; |
| 1394 | + |
| 1395 | + ParseObject.enableSingleInstance(); |
| 1396 | + |
| 1397 | + var objectToReturn = { |
| 1398 | + objectId: 'T01', |
| 1399 | + name: 'Name', |
| 1400 | + other: 'other', |
| 1401 | + tbd: 'exists', |
| 1402 | + className:"Thing", |
| 1403 | + createdAt: '2017-01-10T10:00:00Z', |
| 1404 | + subObject: {key1:"value", key2:"value2", key3:"thisWillGoAway"} |
| 1405 | + }; |
| 1406 | + |
| 1407 | + CoreManager.setQueryController({ |
| 1408 | + find(className, params, options) { |
| 1409 | + return ParsePromise.as({ |
| 1410 | + results: [objectToReturn] |
| 1411 | + }); |
| 1412 | + } |
| 1413 | + }); |
| 1414 | + |
| 1415 | + var q = new ParseQuery("Thing"); |
| 1416 | + var testObject; |
| 1417 | + return q.find().then((results) => { |
| 1418 | + testObject = results[0]; |
| 1419 | + |
| 1420 | + expect(testObject.get("name")).toBe("Name"); |
| 1421 | + expect(testObject.get("other")).toBe("other"); |
| 1422 | + expect(testObject.has("tbd")).toBe(true); |
| 1423 | + expect(testObject.get("subObject").key1).toBe("value"); |
| 1424 | + expect(testObject.get("subObject").key2).toBe("value2"); |
| 1425 | + expect(testObject.get("subObject").key3).toBe("thisWillGoAway"); |
| 1426 | + |
| 1427 | + var q2 = new ParseQuery("Thing"); |
| 1428 | + q2.select("other", "tbd", "subObject.key1", "subObject.key3"); |
| 1429 | + objectToReturn = { objectId: 'T01', other: 'other2', subObject:{key1:"updatedValue"}}; |
| 1430 | + return q2.find(); |
| 1431 | + }).then((results) => { |
| 1432 | + expect(results[0].get("name")).toBe("Name"); //query didn't select this |
| 1433 | + expect(results[0].get("other")).toBe("other2"); //query selected and updated this |
| 1434 | + expect(results[0].has("tbd")).toBe(false); //query selected this and it wasn't returned |
| 1435 | + //sub-objects should work similarly |
| 1436 | + expect(results[0].get("subObject").key1).toBe("updatedValue"); |
| 1437 | + expect(results[0].get("subObject").key2).toBe("value2"); |
| 1438 | + expect(results[0].get("subObject").key3).toBeUndefined(); |
| 1439 | + }).then(() => { |
| 1440 | + expect(testObject.get("name")).toBe("Name"); |
| 1441 | + expect(testObject.get("other")).toBe("other2"); |
| 1442 | + expect(testObject.has("tbd")).toBe(false); |
| 1443 | + expect(testObject.get("subObject").key1).toBe("updatedValue"); |
| 1444 | + expect(testObject.get("subObject").key2).toBe("value2"); |
| 1445 | + expect(testObject.get("subObject").key3).toBeUndefined(); |
| 1446 | + done(); |
| 1447 | + }, (error) => { |
| 1448 | + done.fail(error); |
| 1449 | + }); |
| 1450 | + }); |
| 1451 | + |
| 1452 | + it('overrides cached object with first() results', (done) => { |
| 1453 | + jest.dontMock("../ParseObject"); |
| 1454 | + jest.resetModules(); |
| 1455 | + ParseObject = require('../ParseObject').default; |
| 1456 | + CoreManager = require('../CoreManager'); |
| 1457 | + ParseQuery = require('../ParseQuery').default; |
| 1458 | + |
| 1459 | + ParseObject.enableSingleInstance(); |
| 1460 | + |
| 1461 | + var objectToReturn = { |
| 1462 | + objectId: 'T01', |
| 1463 | + name: 'Name', |
| 1464 | + other: 'other', |
| 1465 | + className:"Thing", |
| 1466 | + createdAt: '2017-01-10T10:00:00Z' |
| 1467 | + }; |
| 1468 | + |
| 1469 | + CoreManager.setQueryController({ |
| 1470 | + find(className, params, options) { |
| 1471 | + return ParsePromise.as({ |
| 1472 | + results: [objectToReturn] |
| 1473 | + }); |
| 1474 | + } |
| 1475 | + }); |
| 1476 | + |
| 1477 | + var q = new ParseQuery("Thing"); |
| 1478 | + var testObject; |
| 1479 | + q.first().then((result) => { |
| 1480 | + testObject = result; |
| 1481 | + |
| 1482 | + expect(testObject.get("name")).toBe("Name"); |
| 1483 | + expect(testObject.get("other")).toBe("other"); |
| 1484 | + |
| 1485 | + objectToReturn = { objectId: 'T01', name: 'Name2'}; |
| 1486 | + var q2 = new ParseQuery("Thing"); |
| 1487 | + return q2.first(); |
| 1488 | + }).then((result) => { |
| 1489 | + expect(result.get("name")).toBe("Name2"); |
| 1490 | + expect(result.has("other")).toBe(false); |
| 1491 | + }).then(() => { |
| 1492 | + expect(testObject.get("name")).toBe("Name2"); |
| 1493 | + expect(testObject.has("other")).toBe(false); |
| 1494 | + done(); |
| 1495 | + }); |
| 1496 | + }); |
| 1497 | + |
| 1498 | + it('does not override unselected fields for first() on select query', (done) => { |
| 1499 | + jest.dontMock("../ParseObject"); |
| 1500 | + jest.resetModules(); |
| 1501 | + ParseObject = require('../ParseObject').default; |
| 1502 | + CoreManager = require('../CoreManager'); |
| 1503 | + ParseQuery = require('../ParseQuery').default; |
| 1504 | + |
| 1505 | + ParseObject.enableSingleInstance(); |
| 1506 | + |
| 1507 | + var objectToReturn = { |
| 1508 | + objectId: 'T01', |
| 1509 | + name: 'Name', |
| 1510 | + other: 'other', |
| 1511 | + tbd: 'exists', |
| 1512 | + className:"Thing", |
| 1513 | + subObject: {key1:"value", key2:"value2", key3:"thisWillGoAway"}, |
| 1514 | + createdAt: '2017-01-10T10:00:00Z', |
| 1515 | + }; |
| 1516 | + |
| 1517 | + CoreManager.setQueryController({ |
| 1518 | + find(className, params, options) { |
| 1519 | + return ParsePromise.as({ |
| 1520 | + results: [objectToReturn] |
| 1521 | + }); |
| 1522 | + } |
| 1523 | + }); |
| 1524 | + |
| 1525 | + var q = new ParseQuery("Thing"); |
| 1526 | + var testObject; |
| 1527 | + return q.first().then((result) => { |
| 1528 | + testObject = result; |
| 1529 | + |
| 1530 | + expect(testObject.get("name")).toBe("Name"); |
| 1531 | + expect(testObject.get("other")).toBe("other"); |
| 1532 | + expect(testObject.has("tbd")).toBe(true); |
| 1533 | + |
| 1534 | + var q2 = new ParseQuery("Thing"); |
| 1535 | + q2.select("other", "tbd", "subObject.key1", "subObject.key3"); |
| 1536 | + objectToReturn = { objectId: 'T01', other: 'other2', subObject:{key1:"updatedValue"}}; |
| 1537 | + return q2.first(); |
| 1538 | + }).then((result) => { |
| 1539 | + expect(result.get("name")).toBe("Name"); //query didn't select this |
| 1540 | + expect(result.get("other")).toBe("other2"); //query selected and updated this |
| 1541 | + expect(result.has("tbd")).toBe(false); //query selected this and it wasn't returned |
| 1542 | + //sub-objects should work similarly |
| 1543 | + expect(result.get("subObject").key1).toBe("updatedValue"); |
| 1544 | + expect(result.get("subObject").key2).toBe("value2"); |
| 1545 | + expect(result.get("subObject").key3).toBeUndefined(); |
| 1546 | + }).then(() => { |
| 1547 | + expect(testObject.get("name")).toBe("Name"); |
| 1548 | + expect(testObject.get("other")).toBe("other2"); |
| 1549 | + expect(testObject.has("tbd")).toBe(false); |
| 1550 | + expect(testObject.get("subObject").key1).toBe("updatedValue"); |
| 1551 | + expect(testObject.get("subObject").key2).toBe("value2"); |
| 1552 | + expect(testObject.get("subObject").key3).toBeUndefined(); |
| 1553 | + done(); |
| 1554 | + }, (error) => { |
| 1555 | + done.fail(error); |
| 1556 | + }); |
| 1557 | + }); |
1335 | 1558 | });
|
0 commit comments