<commit_before>import * as chai from 'chai';
const { assert } = chai;

import gql from 'graphql-tag';

import mockWatchQuery from './mocks/mockWatchQuery';
import { ObservableQuery } from '../src/ObservableQuery';

// I'm not sure why mocha doesn't provide something like this, you can't
// always use promises
const wrap = (done: Function, cb: (...args: any[]) => any) => (...args: any[]) => {
  try {
    return cb(...args);
  } catch (e) {
    done(e);
  }
};

describe('ObservableQuery', () => {
  describe('setVariables', () => {
    const query = gql`
      query($id: ID!){
        people_one(id: $id) {
          name
        }
      }
    `;
    const variables = { id: 1 };
    const differentVariables = { id: 2 };
    const dataOne = {
      people_one: {
        name: '<NAME>',
      },
    };
    const dataTwo = {
      people_one: {
        name: '<NAME>',
      },
    };

    it('reruns query if the variables change', (done) => {
      const observable: ObservableQuery = mockWatchQuery({
        request: { query, variables },
        result: { data: dataOne },
      }, {
        request: { query, variables: differentVariables },
        result: { data: dataTwo },
      });

      let handleCount = 0;
      observable.subscribe({
        next: wrap(done, result => {
          handleCount++;

          if (handleCount === 1) {
            assert.deepEqual(result.data, dataOne);
            observable.setVariables(differentVariables);
          } else if (handleCount === 2) {
            assert.deepEqual(result.data, dataTwo);
            done();
          }
        }),
      });
    });


    it('does not rerun query if variables do not change', (done) => {
      const observable: ObservableQuery = mockWatchQuery({
        request: { query, variables },
        result: { data: dataOne },
      }, {
        request: { query, variables },<commit_msg>Added a failing test for setVariables.<commit_after>import * as chai from 'chai';
const { assert } = chai;

import gql from 'graphql-tag';

import mockWatchQuery from './mocks/mockWatchQuery';
import { ObservableQuery } from '../src/ObservableQuery';

// I'm not sure why mocha doesn't provide something like this, you can't
// always use promises
const wrap = (done: Function, cb: (...args: any[]) => any) => (...args: any[]) => {
  try {
    return cb(...args);
  } catch (e) {
    done(e);
  }
};

describe('ObservableQuery', () => {
  describe('setVariables', () => {
    const query = gql`
      query($id: ID!){
        people_one(id: $id) {
          name
        }
      }
    `;
    const variables = { id: 1 };
    const differentVariables = { id: 2 };
    const dataOne = {
      people_one: {
        name: '<NAME>',
      },
    };
    const dataTwo = {
      people_one: {
        name: '<NAME>',
      },
    };

    it('reruns query if the variables change', (done) => {
      const observable: ObservableQuery = mockWatchQuery({
        request: { query, variables },
        result: { data: dataOne },
      }, {
        request: { query, variables: differentVariables },
        result: { data: dataTwo },
      });

      let handleCount = 0;
      observable.subscribe({
        next: wrap(done, result => {
          handleCount++;

          if (handleCount === 1) {
            assert.deepEqual(result.data, dataOne);
            observable.setVariables(differentVariables);
          } else if (handleCount === 2) {
            assert.deepEqual(result.data, dataTwo);
            done();
          }
        }),
      });
    });

    it('reruns observer callback if the variables change but data does not', (done) => {
      const observable: ObservableQuery = mockWatchQuery({
        request: { query, variables },
        result: { data: dataOne },
      }, {
        request: { query, variables: differentVariables },
        result: { data: dataOne },
      });

      let handleCount = 0;
      observable.subscribe({
        next: wrap(done, result => {
          console.trace();
          handleCount++;

          if (handleCount === 1) {
            assert.deepEqual(result.data, dataOne);
            observable.setVariables(differentVariables);
          } else if (handleCount === 2) {
            assert.deepEqual(result.data, dataOne);
            done();
          }
        }),
      });
    });

    it('does not rerun query if variables do not change', (done) => {
      const observable: ObservableQuery = mockWatchQuery({
        request: { query, variables },
        result: { data: dataOne },
      }, {
        request: { query, variables },
{'fraction_non_alphanumeric': 0.14616561066200567, 'fraction_numerical': 0.002840288398514311, 'mean_word_length': 2.0459081836327346, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7023397', 'n_tokens_mistral': 1326, 'n_tokens_neox': 1286, 'n_words': 350}